简体   繁体   中英

@OneToOne(mappedBy) causes column to not be created

I am trying to learn JPA and Hibernate. When trying to use the mappedBy annotation to map a access card to an owner of it, the column wont create. I am new to stack overflow so forgive me if I am doing this wrong.

Here is my Employee class

@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

Here is my AccessCard class

@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

Here is my JpaStarterMain class used to create the objects and persist them

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();

I get this in the console

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 (?, ?, ?, ?)

for some reason, when getting rid of the mappedBy in the @OneToOne annotation, owner_id is added into the database under the ACCESS_CARD table, but when using mappedBy, it doesn't work anymore! see below

@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

Then I get this in the console

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 (?, ?, ?, ?, ?)

any help is appreciate thanks!

When you use mappedBy , it means that the relation is persisted on the other side of the relation. This is sufficient in terms of data persistence. You can find the same behaviour with a OneToMany relation where the mappedBy attribute gives the name of the corresponding member on the other class annotated with ManytoOne . In this second case, it is more obvious that the persistence is on the manyToOne side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM