插入JPA集合而不加载它 [英] Insert to JPA collection without loading it
本文介绍了插入JPA集合而不加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用这样的代码为我的实体中的集合添加一个新条目。
player = em .find(Player.class,playerId);
player.getAvatarAttributeOwnership()。add(new AvatarAttributeOwnership(...));
它可以工作,但每次我想添加一个项目时,整个集合都会被加载。 p>
- 有没有一种方法(可能有查询)添加项目而不加载其余项目?在SQL中,它可能类似于
INSERT INTO AvatarAttributeOwnership(player,data,...)VALUES({player},...);
-
目前唯一性由Set 和
AvatarAttributeOwnership.equals
的合约维护,但我假设那将不再有效。我该如何执行它? 我使用JPA2 + Hibernate。代码: - Is there a way (with a query maybe) to add the item without loading the rest? In SQL it would be something like
INSERT INTO AvatarAttributeOwnership(player, data, ...) VALUES({player}, ...);
Currently uniqueness is maintained by the contract ofSet
andAvatarAttributeOwnership.equals
, but I assume that won't work anymore. How can I enforce it anyway?
@Entity
公共类Player实现Serializable {
@Id
@GeneratedValue
私人长ID;
@ElementCollection(fetch = FetchType.LAZY)
//编辑:#2
@CollectionTable(uniqueConstraints = @ UniqueConstraint(columnNames = {Player_id,gender ,type,attrId}))
Set< AvatarAttributeOwnership> ownedAvatarAttributes;
...
}
@Embeddable
public class AvatarAttributeOwnership实现Serializable {
@Column (nullable = false,长度= 6)
@Enumerated(EnumType.STRING)
私人性别性别;
@Column(nullable = false,长度= 20)
私有字符串类型;
@Column(nullable = false,length = 50)
private String attrId;
@Column(nullable = false)
私有日期自;
@Override
public boolean equals(Object obj){
if(this == obj)return true;
if(obj == null)return false;
if(getClass()!= obj.getClass())返回false;
AvatarAttributeOwnership other =(AvatarAttributeOwnership)obj;
if(!attrId.equals(other.attrId))return false;
if(gender!= other.gender)返回false;
if(!type.equals(other.type))返回false;
返回true;
}
...
}
解决方案
请尝试额外-lazy collection :
$ b $ pre $ @LazyCollection(LazyCollectionOption.EXTRA)
I'm currently using code like this to add a new entry to a set in my entity.
player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));
It works, but every time I want to add one item, the whole set is loaded.
I'm using JPA2+Hibernate. Code:
@Entity
public class Player implements Serializable {
@Id
@GeneratedValue
private long id;
@ElementCollection(fetch=FetchType.LAZY)
// EDIT: answer to #2
@CollectionTable(uniqueConstraints=@UniqueConstraint(columnNames={"Player_id","gender","type","attrId"}))
Set<AvatarAttributeOwnership> ownedAvatarAttributes;
...
}
@Embeddable
public class AvatarAttributeOwnership implements Serializable {
@Column(nullable=false,length=6)
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(nullable=false,length=20)
private String type;
@Column(nullable=false,length=50)
private String attrId;
@Column(nullable=false)
private Date since;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
AvatarAttributeOwnership other = (AvatarAttributeOwnership) obj;
if (!attrId.equals(other.attrId)) return false;
if (gender != other.gender) return false;
if (!type.equals(other.type)) return false;
return true;
}
...
}
解决方案
@LazyCollection(LazyCollectionOption.EXTRA)
这篇关于插入JPA集合而不加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文