英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论