创建 Hibernate 代理对象

huangapple go评论66阅读模式
英文:

Creating Hibernate proxy objects

问题

public class Account {
    private int pk;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "profileFK")
    private Profile profile;
}

public class Profile {
    private int pk;
    private String name;
}

我有一个账户和一个 profilePk我希望能够在不从数据库获取 profile 的情况下设置关联有没有办法在我拥有它的 PK 时创建一个 Hibernate 代理

我尝试了只使用包含 PK 的新 Profile这在保存到数据库时可以工作但它也会将对象放入 Hibernate 缓存中

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);

Profile profile = new Profile();
profile.setPk(dbProfile.getPk());
account.setProfile(profile);
accountDao.save(account);

Account dbAccount = accountDao.get(account.getPk());
assertNull(dbAccount.getProfile().getName());
accountDao.refresh(dbAccount);
assertEquals("name", dbAccount.getProfile().getName());

但我想要做类似这样的事情

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);

account.setProfile(Hibernate.newProxy(Profile.class, dbProfile.getPk()));
accountDao.save(account);

Account dbAccount = accountDao.get(account.getPk());
assertEquals("name", dbAccount.getProfile().getName());

或者是否还有其他选项在保存账户之前我不必从数据库获取 profile我们正在将数百个使用 mapstruct 生成的旧对象迁移到 jpa像这样做会使通用解决方案变得更加容易
英文:
public class Account {
private int pk;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profileFK")
private Profile profile;
}
public class Profile {
private int pk;
private String name;
}

I have an account and a profilePk and I want to be able to set the relation without getting the profile from the database, is there a way to create a HibernateProxy when I have the PK for it?

I tried just making a new Profile with just the PK in it which works when saving to db but it also puts the "empty" object in the hibernate cache.

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);
Profile profile = new Profile();
profile.setPk(dbProfile.getPk());
account.setProfile(profile);
accountDao.save(account);
Account dbAccount = accountDao.get(account.getPk());
assertNull(dbAccount.getProfile().getName());
accountDao.refresh(dbAccount);
assertEquals("name", dbAccount.getProfile().getName());

But I want to do something like this

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);
account.setProfile(Hibernate.newProxy(Profile.class, dbProfile.getPk()));
accountDao.save(account);
Account dbAccount = accountDao.get(account.getPk());
assertEquals("name", dbAccount.getProfile().getName());

Or is there another option where I don't have to get the profile from DB before saving account? We are migrating hundreds of generated old objects with mapstruct to jpa and something like this would make a generic solution a lot easier.

答案1

得分: 1

根据 Hibernate 文档,你应该这样做:

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);

account.setProfile(entityManager.getReference(Profile.class, dbProfile.getPk()));
accountDao.save(account);
英文:

According to the hibernate documentation you should do something like this:

Profile dbProfile = new Profile();
dbProfile.setName("name");
profileDao.create(dbProfile);

account.setProfile(entityManager.getReference(Profile.class, dbProfile.getPk()));
accountDao.save(account);

huangapple
  • 本文由 发表于 2020年4月8日 18:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61098477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定