我正在尝试学习JPA和Hibernate。当尝试使用mappedBy注解将访问卡Map到它的所有者时,列不会创建。我是堆栈溢出的新手,所以如果我做错了请原谅我。
下面是我的Employee类
@Entity
@Table(name = "EMPLOYEE_DATA")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
@Column(name = "EMPLOYEE_NAME")
private String name;
@Column(unique = true, length = 9, nullable = false, updatable = false)
private String ssn;
@Column(nullable = true)
private int age;
@Temporal(TemporalType.DATE)
private Date dob;
@Enumerated(EnumType.STRING)
private EmployeeType type;
@OneToOne
private AccessCard card;
@Transient
public String debugString;
//getters and setters, toString
下面是我的AccessCard类
@Entity
@Table(name = "ACCESS_CARD")
public class AccessCard {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
@Temporal(TemporalType.DATE)
private Date issueDate;
private boolean isActive;
private String firmwareVersion;
@OneToOne(mappedBy = "card")
private Employee owner;
//getters and setters, toString
下面是用于创建对象和持久化对象的JpaStarterMain类
public class JpaStarterMain {
public static void main(String[] args) {
Employee employee2 = new Employee();
employee2.setType(EmployeeType.FULL_TIME);
employee2.setAge(24);
employee2.setDob(new GregorianCalendar(1998, Calendar.OCTOBER, 20).getTime());
employee2.setName("Louie Jenkins");
employee2.setSsn("123456789");
Employee employee1 = new Employee();
employee1.setType(EmployeeType.PART_TIME);
employee1.setAge(21);
employee1.setDob(new GregorianCalendar(2001, Calendar.OCTOBER, 5).getTime());
employee1.setName("Gigi Vad");
employee1.setSsn("987654321");
AccessCard card2 = new AccessCard();
card2.setFirmwareVersion("2.7.0");
card2.setIssueDate(new Date());
card2.setActive(true);
card2.setOwner(employee2);
employee2.setCard(card2);
AccessCard card1 = new AccessCard();
card1.setFirmwareVersion("2.7.0");
card1.setIssueDate(new Date());
card1.setActive(false);
card1.setOwner(employee1);
employee1.setCard(card1);
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("test");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(employee1);
entityManager.persist(employee2);
entityManager.persist(card1);
entityManager.persist(card2);
transaction.commit();
entityManager.close();
entityManagerFactory.close();
我在控制台上看到这个
Hibernate: insert into EMPLOYEE_DATA (age, card_id, dob, EMPLOYEE_NAME, ssn, type, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into EMPLOYEE_DATA (age, card_id, dob, EMPLOYEE_NAME, ssn, type, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ACCESS_CARD (firmwareVersion, isActive, issueDate, id) values (?, ?, ?, ?)
Hibernate: insert into ACCESS_CARD (firmwareVersion, isActive, issueDate, id) values (?, ?, ?, ?)
由于某种原因,当去掉@OneToOne注解中的mappedBy时,owner_id被添加到ACCESS_CARD表下的数据库中,但是当使用mappedBy时,它就不再起作用了!请参阅下文
@Entity
@Table(name = "ACCESS_CARD")
public class AccessCard {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
@Temporal(TemporalType.DATE)
private Date issueDate;
private boolean isActive;
private String firmwareVersion;
@OneToOne
private Employee owner;
//getters and setters, toString
然后我在控制台上看到这个
Hibernate: insert into EMPLOYEE_DATA (age, card_id, dob, EMPLOYEE_NAME, ssn, type, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into EMPLOYEE_DATA (age, card_id, dob, EMPLOYEE_NAME, ssn, type, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ACCESS_CARD (firmwareVersion, isActive, issueDate, owner_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into ACCESS_CARD (firmwareVersion, isActive, issueDate, owner_id, id) values (?, ?, ?, ?, ?)
任何帮助都是感激的谢谢!
1条答案
按热度按时间dxpyg8gm1#
当你使用
mappedBy
时,它意味着关系在关系的另一端被持久化。这对于数据持久化来说是足够的。你可以在OneToMany
关系中找到相同的行为,其中mappedBy
属性给出了用ManytoOne
注解的另一个类上的对应成员的名称。在第二种情况下,更明显的是持久性在manyToOne
侧。