ポイント:
1、Joinテーブル:JOIN_DELEGATE_EVENT
2、EventとDelegateテーブル
Event.java
package com.test; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Event { private int eventId; private String eventName; private List<Delegate> delegates = new ArrayList<Delegate>(); @Id @GeneratedValue public int getEventId() { return eventId; } public void setEventId(int eventId) { this.eventId = eventId; } public String getEventName() { return eventName; } public void setEventName(String eventName) { this.eventName = eventName; } @ManyToMany @JoinTable(name = "JOIN_DELEGATE_EVENT", joinColumns = { @JoinColumn(name = "eventId") }, inverseJoinColumns = { @JoinColumn(name = "delegateId") }) public List<Delegate> getDelegates() { return delegates; } public void setDelegates(List<Delegate> delegates) { this.delegates = delegates; } }
Delegate.java
package com.test; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Delegate { private int delegateId; private String delegateName; private List<Event> events = new ArrayList<Event>(); @Id @GeneratedValue public int getDelegateId() { return delegateId; } public void setDelegateId(int delegateId) { this.delegateId = delegateId; } public String getDelegateName() { return delegateName; } public void setDelegateName(String delegateName) { this.delegateName = delegateName; } @ManyToMany @JoinTable(name = "JOIN_DELEGATE_EVENT", joinColumns = { @JoinColumn(name = "delegateId") }, inverseJoinColumns = { @JoinColumn(name = "eventId") }) public List<Event> getEvents() { return events; } public void setEvents(List<Event> events) { this.events = events; } }
EventTest.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 EventTest { 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 { Delegate d1 = new Delegate(); d1.setDelegateName("d1"); Delegate d2 = new Delegate(); d2.setDelegateName("d2"); Delegate d3 = new Delegate(); d3.setDelegateName("d3"); Delegate d4 = new Delegate(); d4.setDelegateName("d4"); Event e1 = new Event(); e1.setEventName("java"); Event e2 = new Event(); e2.setEventName("c#"); Event e3 = new Event(); e3.setEventName("vb"); e1.getDelegates().add(d1); e1.getDelegates().add(d2); e2.getDelegates().add(d2); e2.getDelegates().add(d3); e3.getDelegates().add(d3); e3.getDelegates().add(d4); entityManager.persist(e1); entityManager.persist(e2); entityManager.persist(e3); entityManager.persist(d1); entityManager.persist(d2); entityManager.persist(d3); entityManager.persist(d4); entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); } } }
Delegate.sql
create table Delegate ( delegateId int4 not null, delegateName varchar(255), primary key (delegateId) )
delegateid | delegatename |
4 | d1 |
5 | d2 |
6 | d3 |
7 | d4 |
create table Event ( eventId int4 not null, eventName varchar(255), primary key (eventId) )
eventid | eventname |
1 | java |
2 | c# |
3 | vb |
create table JOIN_DELEGATE_EVENT ( delegateId int4 not null, eventId int4 not null )
delegateid | eventid |
4 | 1 |
5 | 1 |
5 | 2 |
6 | 2 |
6 | 3 |
7 | 3 |
0 件のコメント:
コメントを投稿