这里是我的方法:
// composite primary key
public class TripId implements Serializable {
private int id;
private long trip_no;
public TripId(int id, long trip_no) {
this.id = id;
this.trip_no = trip_no;
}
// getters, setters, hashCode, equals
}
编辑:我不得不更新从下面的评论Welshbard的问题后,实体模型。的变化是:
Person
实体:Map<String, Trip> trips;
取代List<Trip> trips;
Trip
实体:String state;
字段添加作为替代trips
键从Person
实体
- 构造/ setter方法/代码段用于填充数据/输出模式是相应更新
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
private List<Trip> trips;
public Person() {
this.trips = new List<>();
}
public void setTrips(List<Trip> trips) {
this.trips = trips;
}
...
}
@Entity
@IdClass(TripId.class)
public class Trip {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Id
private long trip_no;
private String state;
@ManyToOne
private Person person;
public Trip() {
}
public Trip(String state, long trip_no, Person person) {
this.state = state;
this.trip_no = trip_no;
this.person = person;
}
...
}
现在,我们可以利用上面的ORM的,并填充一些数据:
Person person1 = new Person();
Person person2 = new Person();
Trip mn1Trip1 = new Trip("MN", 1, person1);
Trip wi1Trip1 = new Trip("WI", 1, person1);
Trip nd2Trip1 = new Trip("ND", 2, person1);
Trip mt2Trip1 = new Trip("MT", 2, person1);
Trip ia1Trip2 = new Trip("IA", 1, person2);
// more than one trip by a person goes to the same state
Trip mn2Trip1 = new Trip("MN", 2, person1);
Trip ia2Trip2 = new Trip("IA", 2, person2);
Trip mn1Trip2 = new Trip("MN", 1, person2);
person1.setTrips(Arrays.asList(mn1Trip1, wi1Trip1, nd2Trip1, mt2Trip1, mn2Trip1));
person2.setTrips(Arrays.asList(ia1Trip2, ia2Trip2, mn1Trip2));
em.getTransaction().begin();
em.persist(person1);
em.persist(person2);
em.getTransaction().commit();
最后,我们可以查询数据库,看的模式以及如何被填充的信息:
Person table
============
id
--
1
7
Trip table
===========
id trip_no person_id state
-- ------- --------- -----
9 2 IA 7
4 2 ND 1
8 1 IA 7
6 2 MN 1
2 1 MN 1
10 1 MN 7
3 1 WI 1
5 2 MT 1
注意:ids是不同的,但架构已正确创建并且数据按预期填充。
至于从下面的评论的问题:
// Person class have only trips associated with that person
String jpql1 = "SELECT p.trips FROM Person p WHERE p.id = 1";
List<Trip> trips = em.createQuery(jpql1, Trip.class)
.getResultList();
// the code can look at the Trips to find out the trip_no
String jpql2 = "SELECT t.trip_no FROM Trip t JOIN Person p " +
"WHERE p.id = :id AND t.state = :state";
List<Long> trip_nos = em.createQuery(jpql2, Long.class)
.setParameter("id", 1)
.setParameter("state", "MN")
.getResultList();
关于第二个问题:
可以毫不JPA 2做些什么呢?
您可以尝试使用普通的旧JDBC并使用java.sql.Statement
来插入SQL语句。
“@ OneToMany”注释和朋友描述数据库实体之间的关系。我无法确定Map是什么实体。 –
mpkorstanje
2015-02-09 23:28:36
Map映射按状态键入的一组行程数据。因此,该地图将包含所有具有相同TRIP_NO的行程和由该行程所处状态键入的人员。因此,'person1.getTrips()。get(1)是{{“MN”,tripdata1},{“WI “,tripdata2}}'。 'person1.getTrips()。get(2)是{{“ND”,tripdata1},{“MT”,tripdata2}}'。 'person2.getTrips()。get(1)是{{“IA”,tripdata1}}'。 –
2015-02-09 23:42:39
啊我明白了。那么你的TRIP表中有太多的信息。它包含旅行和旅行目的地实体。看到我发布的答案。 – mpkorstanje 2015-02-10 00:03:59