Hibernate ManyToOne vs OneToOne

They look exactly the same on schema but there is difference on Hibernate Layer. If you try something like that: Address address = new Address(); Order order1 = new Order(); order1.setAddress(address); Order order2 = new Order(); order2.setAddress(address); save(); Everything will be OK. But, after save if you try get Order: @OneToOne case: org.hibernate.HibernateException: More than …

Read more

JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert=”false” update=”false”)

I am not really sure about your question (the meaning of “empty table” etc, or how mappedBy and JoinColumn were not working). I think you were trying to do a bi-directional relationships. First, you need to decide which side “owns” the relationship. Hibernate is going to setup the relationship base on that side. For example, …

Read more

JPA OneToMany and ManyToOne throw: Repeated column in mapping for entity column (should be mapped with insert=”false” update=”false”)

I am not really sure about your question (the meaning of “empty table” etc, or how mappedBy and JoinColumn were not working). I think you were trying to do a bi-directional relationships. First, you need to decide which side “owns” the relationship. Hibernate is going to setup the relationship base on that side. For example, …

Read more

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

As an answer to okutane, please see snippet: @JoinColumn(name = “car_id”, insertable = false, updatable = false) @ManyToOne(targetEntity = Car.class, fetch = FetchType.EAGER) private Car car; @Column(name = “car_id”) private Long carId; So what happens here is that when you want to do an insert/update, you only populate the carId field and perform the insert/update. …

Read more

What is the meaning of the CascadeType.ALL for a @ManyToOne JPA association

The meaning of CascadeType.ALL is that the persistence will propagate (cascade) all EntityManager operations (PERSIST, REMOVE, REFRESH, MERGE, DETACH) to the relating entities. It seems in your case to be a bad idea, as removing an Address would lead to removing the related User. As a user can have multiple addresses, the other addresses would …

Read more