update jpa shared key

This commit is contained in:
Loredana 2020-10-20 12:33:13 +03:00
parent d6d7661771
commit e16eef7726
3 changed files with 6 additions and 3 deletions

View File

@ -8,7 +8,7 @@ public class Application {
public static void main(String[] args) {
EntityManager entityManager = getJpaEntityManager();
User user = entityManager.find(User.class, 1);
User user = entityManager.find(User.class, 1l);
System.out.println(user);
entityManager.getTransaction().begin();
user.setName("John");

View File

@ -1,9 +1,9 @@
package com.baeldung.hibernate.onetoone.sharedkeybased;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@ -13,7 +13,7 @@ import javax.persistence.Table;
public class Address {
@Id
@Column(name = "id")
@Column(name = "user_id")
private Long id;
@Column(name = "street")
@ -24,6 +24,7 @@ public class Address {
@OneToOne
@MapsId
@JoinColumn(name = "user_id")
private User user;
public Long getId() {

View File

@ -8,6 +8,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@ -22,6 +23,7 @@ public class User {
private String userName;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Address address;
public Long getId() {