插入到JPA集合而不加载它

发布于 2024-11-26 13:39:33 字数 1729 浏览 0 评论 0原文

我目前正在使用这样的代码将新条目添加到我的实体中的集合中。

player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));

它有效,但每次我想添加一个项目时,都会加载整套项目。

  1. 有没有办法(可能需要查询)添加该项目而不加载其余项目?在 SQL 中,它类似于 INSERT INTO AvatarAttributeOwnership(player, data, ...) VALUES({player}, ...);
  2. 当前唯一性由 SetAvatarAttributeOwnership.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;
    }

    ...

}

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.

  1. 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}, ...);
  2. Currently uniqueness is maintained by the contract of Set and AvatarAttributeOwnership.equals, but I assume that won't work anymore. How can I enforce it anyway?

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;
    }

    ...

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2024-12-03 13:39:33

尝试额外惰性集合

@LazyCollection(LazyCollectionOption.EXTRA)

Try extra-lazy collections:

@LazyCollection(LazyCollectionOption.EXTRA)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文