[英]Hibernate Triple Many-To-Many Join
I need some help modeling a table that has a many to many relationship to another table which has a many to many relationship to a third table. 我需要一些帮助来建模一个与另一个表有多对多关系的表,这个表与第三个表有很多关系。
To illustrate, say I have three tables relating to automobiles: Dealership, Make, and Model. 为了说明,我说有三张与汽车有关的表:经销商,品牌和型号。 A Dealership has a many to many relationship with Make (ie: Honda, Ford, etc) and a Make has a many to many relationship with the Model (ie: Passport, Accord, etc).
经销商与Make有许多关系(即:本田,福特等),而且Make与模型有许多关系(即:Passport,Accord等)。 (Note, in real life I'm sure Make's and Model's don't have a many to many relationship, but I'm using this relationship for illustration purposes).
(注意,在现实生活中,我确信Make和Model没有多对多的关系,但我将这种关系用于说明目的)。 Given a particular Dealership, I'd like to get all the Make's they carry, and from each Make I'd like to get all the Model's of that Make that the Dealership carries.
鉴于一个特定的经销商,我想得到他们携带的所有Make,并且从每个Make我想得到经销商所带的那个Make的所有型号。 They may not carry every Model of a particular Make, so it should only return Model's they carry.
它们可能不会携带特定品牌的每个型号,因此它应该只返回它们携带的型号。
Dealership
----------------
1 - Motor Experts
2 - Auto Sales
Make
----------------
1 - Honda
2 - Ford
Model
----------------
1 - Passport
2 - Accord
3 - Taurus
I know how to do a many-to-many relationship between Dealership and Make and a many-to-many relationship between a Make and Model. 我知道如何在Dealership和Make之间建立多对多的关系,以及Make和Model之间的多对多关系。 The problem is I don't know how to relate only the Models that the Dealership carries.
问题是我不知道如何仅与经销商所携带的模型相关联。
What is the best way to model the join tables and what would the JPA/Hibernate annotations be on my java model objects? 对连接表建模的最佳方法是什么?JPA / Hibernate注释对我的java模型对象有什么影响?
I'd almost say that you're missing an entity called "car" which would have "Make, Model and Year". 我几乎会说你错过了一个名为“car”的实体,它将拥有“Make,Model和Year”。 Dealerships would then have Cars.
经销商将拥有汽车。 You would no longer have the 3-way many-to-many.
你将不再拥有3对多对多。 But given that it is just an example you could model the relationship as its own entity, let's say "Inventory", which would have a Dealership, Make, Model and any additional attributes associated with it that you'd like (eg Year, Quantity)
但鉴于它只是一个例子,你可以将关系建模为自己的实体,让我们说“库存”,它将拥有经销商,品牌,型号以及与之相关的任何其他属性(例如年份,数量) )
Inventory (Dealership, Make, Model)
---------
1, 1, 1 -- (Motor Exports, Honda, Passport)
1, 2, 3 -- (Motor Exports, Fort, Taurus)
etc...
In Hibernate you model Inventory as its own entity with an embedded primary key. 在Hibernate中,您将Inventory建模为具有嵌入式主键的自己的实体。 In abbreviated form, it would look like this:
缩写形式,它看起来像这样:
@Entity
public class Inventory {
@EmbeddedId
public InventoryId id;
// additional attributes
@MapsId("dealership")
@ManyToOne
public Dealership dealership;
@MapsId("make")
@ManyToOne
public Make make;
@MapsId("model")
@ManyToOne
public Model model;
}
@Embeddable
public class InventoryId implements Serializable {
@ManyToOne public Dealership dealership;
@ManyToOne public Make make;
@ManyToOne public Model model;
// Don't forget to implement equals and hashcode
}
@Entity
public class Dealership {
@Id @GeneratedValue public Long id;
@Basic public String name;
@OneToMany(mappedBy="dealership") public List<Inventory> inventory;
}
// classes Make and Model follow the same pattern as Dealership
Then to run a query for all models that a particular dealership has in inventory you would use: 然后运行查询特定经销商在库存中使用的所有模型:
List<Model> stock = em.createQuery(
"select distinct inv.model "+
"from Dealership d "+
"join d.inventory inv "+
"where d.name = :dealer",
Model.class)
.setParameter("dealer", "Motor Exports")
.getResultList();
The dealership-model relationship is not direct; 经销商模式关系不直接; it only exists by virtue of the dealership-make and make-model relationships.
它只存在于经销商制造和制造模型的关系中。 This, it's not appropriate to specify it as a property of the dealership.
这样,将其指定为经销商的财产是不合适的。 Rather, you should do a JPQL query to find models available from a dealership.
相反,您应该执行JPQL查询以查找经销商提供的模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.