FKでつながります。
Person.java
package com.test; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Person { private int personId; private String personName; private PersonDetail pDetail; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "pDetail_FK") public PersonDetail getpDetail() { return pDetail; } public void setpDetail(PersonDetail pDetail) { this.pDetail = pDetail; } @Id @GeneratedValue public int getPersonId() { return personId; } public void setPersonId(int personId) { this.personId = personId; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } }
PersonDetail.java
package com.test; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class PersonDetail { private int personDetailId; private String zipCode; private String job; private double income; private Person person; @OneToOne(mappedBy = "pDetail", cascade = CascadeType.ALL) public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } @Id @GeneratedValue @Column(name = "detailId_PK") public int getPersonDetailId() { return personDetailId; } public void setPersonDetailId(int personDetailId) { this.personDetailId = personDetailId; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public double getIncome() { return income; } public void setIncome(double income) { this.income = income; } }
PersonTest.java
package com.test; import static org.junit.Assert.*; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class PersonTest { static EntityManagerFactory entityManagerFactory; @BeforeClass public static void setUpBeforeClass() throws Exception { entityManagerFactory = Persistence .createEntityManagerFactory("org.hibernate.tutorial.jpa"); } @AfterClass public static void tearDownAfterClass() throws Exception { entityManagerFactory.close(); } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { EntityManager entityManager = entityManagerFactory .createEntityManager(); entityManager.getTransaction().begin(); try { Person alex = new Person(); alex.setPersonName("Alex Berry"); // 日本語 PersonDetail alexDetail = new PersonDetail(); alexDetail.setZipCode("20001"); alexDetail.setJob("プログラマ"); alexDetail.setIncome(5000.78); alex.setpDetail(alexDetail); entityManager.persist(alex); entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); // TODO: handle exception } } }
Person.sql
create table Person ( personId int4 not null, personName varchar(255), pDetail_FK int4, primary key (personId) )
personid | personname | pdetail_fk |
1 | Alex Berry | 2 |
PersonDetail.sql
create table PersonDetail ( detailId_PK int4 not null, income float8 not null, job varchar(255), zipCode varchar(255), primary key (detailId_PK) )
detailid_pk | income | job | zipcode |
2 | 5000.78 | プログラマ | 20001 |
0 件のコメント:
コメントを投稿