Java 插入到JPA集合而不加载它

Java 插入到JPA集合而不加载它,java,hibernate,jpa,fetch,jpql,Java,Hibernate,Jpa,Fetch,Jpql,我目前正在使用这样的代码向实体中的集合添加一个新条目 player = em.find(Player.class, playerId); player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...)); 它可以工作,但是每次我想添加一个项目时,整个集合都会被加载 是否有一种方法(可能带有查询)可以在不加载其余内容的情况下添加项目?在SQL中,它类似于将插入到AvatarAttributeOwnership(

我目前正在使用这样的代码向实体中的集合添加一个新条目

player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));
它可以工作,但是每次我想添加一个项目时,整个集合都会被加载

  • 是否有一种方法(可能带有查询)可以在不加载其余内容的情况下添加项目?在SQL中,它类似于将
    插入到AvatarAttributeOwnership(player,data,…)值({player},…)
  • 目前,唯一性由
    Set
    avatartributeownership.equals
    的契约来维护,但我认为这不再有效。我怎样才能执行它呢
  • 我正在使用JPA2+Hibernate。代码:

    @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;
        }
    
        ...
    
    }
    
    @实体
    公共类播放器实现可序列化{
    @身份证
    @生成值
    私人长id;
    @ElementCollection(fetch=FetchType.LAZY)
    //编辑:对#2的回答
    @CollectionTable(uniqueConstraints=@UniqueConstraint(columnNames={“玩家id”、“性别”、“类型”、“属性”}))
    设置OwnedAvataAttribute;
    ...
    }
    @可嵌入
    公共类AvatarAttributeOwnership实现可序列化{
    @列(nullable=false,长度=6)
    @枚举(EnumType.STRING)
    私人性别;
    @列(nullable=false,长度=20)
    私有字符串类型;
    @列(nullable=false,长度=50)
    私有字符串属性;
    @列(nullable=false)
    私人日期自;
    @凌驾
    公共布尔等于(对象obj){
    如果(this==obj)返回true;
    if(obj==null)返回false;
    如果(getClass()!=obj.getClass())返回false;
    AvatarAttributeOwnership other=(AvatarAttributeOwnership)obj;
    如果(!attrId.equals(other.attrId))返回false;
    如果(gender!=other.gender)返回false;
    如果(!type.equals(other.type))返回false;
    返回true;
    }
    ...
    }
    
    试试:


    你确认我的建议有效吗?@Bozho还没有,因为我目前需要专注于使其他事情有效,而不是优化它,但我假设它有效。我也假设,但我没有尝试过,所以你现在可以取消标记接受的答案:)
    @LazyCollection(LazyCollectionOption.EXTRA)