Home Storing pre-defined table reference in Hibernate entity
Reply: 0

Storing pre-defined table reference in Hibernate entity

user1051 Published in May 23, 2018, 8:41 pm

Is it possible in Hibernate to save parent entity with child reference entity (One-to-one reference) that is pre-defined and shall never be changed?

For example, let's say we have a class Car that has a property Engine.

Engine is a class that is stored in a table that was populated during start-up of the application (using SQL).

We created the Car object and set the Engine property to map the values for one of the entries in Engine table.

If we try save the Car object, Hibernate will try to save the Engine object (entity) as well, which will throw an error that given ID already exists.

Is it possible to keep the reference to Engine table, without saving the Engine object?

Car class would look like this:

public class Car {
  @Column(name = "CAR_ID")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long carId;

  @JoinColumn(name = "ENGINE_ID", updatable = false)
  @OneToOne(optional = false)
  private Engine engine;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313694 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO