Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA many-to-one relation - need to save only Id

I have 2 classes: Driver and Car. Cars table updated in separate process. What I need is to have property in Driver that allows me to read full car description and write only Id pointing to existing Car. Here is example:

@Entity(name = "DRIVER") public class Driver { ... ID and other properties for Driver goes here .....      @ManyToOne(fetch=FetchType.LAZY)     @JoinColumn(name = "CAR_ID")     private Car car;      @JsonView({Views.Full.class})     public Car getCar() {       return car;     }     @JsonView({Views.Short.class})     public long getCarId() {       return car.getId();     }     public void setCarId(long carId) {       this.car = new Car (carId);     }  } 

Car object is just typical JPA object with no back reference to the Driver.

So what I was trying to achieve by this is:

  1. I can read full Car description using detailed JSON View
  2. or I can read only Id of the Car in Short JsonView
  3. and most important, when creating new Driver I just want to pass in JSON ID of the car. This way I dont need to do unnesessery reads for the Car during persist but just update Id.

Im getting following error:

object references an unsaved transient instance - save the transient instance before flushing : com.Driver.car -> com.Car 

I dont want to update instance of the Car in DB but rather just reference to it from Driver. Any idea how to achieve what I want?

Thank you.

UPDATE: Forgot to mention that the ID of the Car that I pass during creation of the Driver is valid Id of the existing Car in DB.

like image 512
Andrei V Avatar asked Jan 13 '15 19:01

Andrei V


People also ask

How does JPA define Many-To-One relationships?

Many-To-One relation between entities: Where one entity (column or set of columns) is/are referenced with another entity (column or set of columns) which contain unique values. In relational databases these relations are applicable by using foreign key/primary key between tables.

How do I persist foreign key in JPA?

Use the getReference call of the entityManager to load customer object using the id and then set that onto the customer history. In most cases this call would return a proxy with just the id embedded, the customer attributes will not be loaded unless some other method of the customer is invoked.

How can we save entity in JPA without primary key?

Sometimes your object or table has no primary key. The best solution in this case is normally to add a generated id to the object and table. If you do not have this option, sometimes there is a column or set of columns in the table that make up a unique value. You can use this unique set of columns as your id in JPA.

How do you persist many-to-many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.


1 Answers

You can do this via getReference call in EntityManager:

EntityManager em = ...; Car car = em.getReference(Car.class, carId);  Driver driver = ...; driver.setCar(car); em.persist(driver); 

This will not execute SELECT statement from the database.

like image 159
scetix Avatar answered Oct 13 '22 22:10

scetix