[英]JPA Mapping a Map of Entities
Coming off my way-too-popular question from yesterday: 从昨天开始我广受欢迎的问题:
Hibernate(JPA) mapping a HashMap Hibernate(JPA)映射HashMap
I'm trying to map a structure that is essentially the following: 我正在尝试映射本质上如下的结构:
Map<User, List< POJOWithComposite >>
Where User
is a basic model, similar to the following: 其中“
User
是基本模型,类似于以下内容:
@Entity
public class User extends Model {
public String username;
public String password;
...
}
and POJOWithComposite
is another entity, that has some mapped entities and some primitives, similar to: POJOWithComposite
是另一个实体,具有一些映射的实体和一些基元,类似于:
@Entity
public class POJOWithComposite extends Model {
public int someIntField;
public OtherModel compositeEntity;
...
}
The structure could be analogous to a shopping cart. 该结构可能类似于购物车。 So the
User
holds the account info, the OtherModel
could be a purchasable item, and POJOWithComposite
is a shopping cart that holds items and quantities, say. 因此,
User
拥有帐户信息, OtherModel
可以是可购买的商品,而POJOWithComposite
是包含商品和数量的购物车。 My analogy breaks down a little when it comes to the main object, but humour me, and let's say for every session we add the shopping cart the user paid for to the list, which will then be archived, say. 当涉及到主要对象时,我的类比略有分解,但令我感到幽默,并且说,对于每个会话,我们将用户支付的购物车添加到列表中,然后将其存档。
Firstly, if the list belongs to each user I could just add it to the User
model, although I feel that this isn't very modular (ie the information isn't really user account information). 首先,如果列表属于每个用户,我可以将其添加到
User
模型中,尽管我认为这不是非常模块化的(即信息不是真正的用户帐户信息)。 ATM, this map sits in a utility-like shopping class, which takes care of matching shopping carts with shoppers, to save me from making the User
model too big. 在ATM上,此地图位于类似于实用程序的购物类中,该类负责将购物车与购物者匹配,以免我将
User
模型过大。 (Is there a better pattern to use here perhaps?) (也许在这里可以使用更好的模式吗?)
Assuming that I continue down this road, Let's try and map this Map
. 假设我继续走这条路,让我们尝试映射此
Map
。 Ideally, I would like to be able to map it as is. 理想情况下,我希望能够原样映射。 Although I have had very little luck with that.
尽管我的运气很少。
I happened upon some talk of mapping ArrayList
easily because it's Serializable
, although didn't manage to implement that here. 我碰巧谈论了映射
ArrayList
因为它是Serializable
,尽管没有在这里实现。
So I made a wrapper entity: 所以我做了一个包装实体:
@Entity
public class POJOWithCompositeList extends Model {
@OneToMany
List<POJOWithComposite> list = new ArrayList<POJOWithComposite>();
}
This works. 这可行。 When I use this entity like this, I can map my new:
当我像这样使用这个实体时,我可以映射我的新实体:
@ManyToMany
Map<User, POJOWithCompositeList>
Although I don't really like this solution because I add another mapped entity, ie the wrapping entity means another table in the database, which means another join every query etc... 尽管我不太喜欢这种解决方案,因为我添加了另一个映射实体,即包装实体意味着数据库中的另一个表,这意味着每个查询都需要另一个联接等。
So my two questions are: 所以我的两个问题是:
Map
in a util class acceptable? Map
粘贴在util类中是否可以接受? There is no point in creating a singleton entity. 创建单例实体没有意义。 You should simply map a one-to-many association between User and POJOWithComposite.
您应该简单地映射User和POJOWithComposite之间的一对多关联。
Get a User (using session.get()
) and call user.getPOJOWithComposites()
to get all the POJOWithComposites of a given user. 获取一个用户(使用
session.get()
)并调用user.getPOJOWithComposites()
以获取给定用户的所有POJOWithComposites。 If you want them for several users, then find these users with HQL, and iterate through the found users and navigate into the association. 如果您希望将其用于多个用户,请使用HQL查找这些用户,然后遍历找到的用户并导航至关联。 The singleton map is the hibernate session.
单例映射是休眠会话。 Use its methods to query the database.
使用其方法查询数据库。
I ended up making a new Entity with @ManyToMany
mappings on the composite parts, something like: 我最终在复合部件上使用
@ManyToMany
映射创建了一个新实体,如下所示:
@Entity
public class POJOWithComposites extends Model {
@ManyToMany
CompositeType compositePtr;
@ManyToMany
CompositeOtherType compositeOtherPtr;
...
}
That way I indeed left this element of complexity out of my User model, since it wasn't part of the core user entity, and allowed an efficient mapping (foreign keys). 这样,我确实将复杂性这一元素排除在我的用户模型之外,因为它不是核心用户实体的一部分,并且允许进行有效的映射(外键)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.