插入与子对象的Hibernate不返回完整的对象 [英] Insert with Hibernate with child objects does not return full object

查看:167
本文介绍了插入与子对象的Hibernate不返回完整的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Spring 3.2和hibernate-core 4.1.4以及hibernate-jpa-2.0.1。应用程序资源文件包含所有正确的对象。

我有一个子对象RoleEntity,它有两个父母:User和Award,所以userId和awardId是外键 - 已存在的键,并且必须存在用于创建角色实体。

  public class RoleEntity implements Serializable 
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =role_id)
private long roleId;

@Column(name =role_description)
private String roleDescription;

@Column(name =role_name)
private String roleName;
$ b @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =user_id)$ b $ private UserEntity user;
$ b @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =award_id)
私人AwardEntity奖;
$ b ... getters / setters
... hash / equals / toString
}

我的DAO插入看起来很简单:



$ public RoleEntity saveRoleEntity (RoleEntity角色)
{
logger.debug(saveRoleEntity:role =+ role);
返回角色;
}

我有单元测试了这个代码,我可以确认当我选择现有的roleId,我可以完全获取加载的对象,用户和奖完全填充。

但是,当我插入新角色时,我执行以下操作:


  1. 将roleId设置为0,以及角色描述...
  2. 创建一个userEntity并仅设置id ,这个ID已经存在,不是新的
  3. 创建一个奖励实体,并设置唯一的ID,这个ID已经存在,不是新的

我可以成功保存,这真是太棒了!为我工作。
并且在新的对象返回中,我可以清楚地看到新的角色返回给我!
所有其他领域RoleEntity,如描述,那里是。

但是,我还想要的是RoleEntity的用户和奖励字段完全填充好像我插入后做了选择。这是可能的吗?

我宁愿不做一个选择的用户和奖表获得这些对象,然后将其分配给这个对象,但如果我有那就好了。



如果我需要提供更多信息,请告诉我。
感谢您的任何帮助。

解决方案

尝试这种方法:



sess.save(role);

sess.flush(); //强制执行SQL INSERT

sess.refresh(role);


I am using Spring 3.2 and hibernate-core 4.1.4 and hibernate-jpa-2.0.1. The application resource file has all the correct objects.

I have a child object RoleEntity, and it has two parents: User and Award, so the userId and the awardId are foreign-keys that already exist, and MUST exist for the role entity to be created.

public class RoleEntity implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "role_id")
    private long roleId;

    @Column(name = "role_description")
    private String roleDescription;

    @Column(name = "role_name")
    private String roleName;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private UserEntity user;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "award_id")
    private AwardEntity award;

     ... getters/setters
     ... hash/equals/toString
}

My DAO insert looks very simple:

@Override
public RoleEntity saveRoleEntity(RoleEntity role)
{
     logger.debug("saveRoleEntity: role=" + role);
     return role;
}

I have unit tested this code, and I can confirm that when I select an existing roleId, I can fully get back the loaded object, with User and Award fully populated.

However, when I insert new role, I do the following:

  1. set roleId to 0, and the role description ...
  2. create a userEntity and set the id only, this id already exists and is not new
  3. create a awardEntity and set the id only, this id already exists and is not new

I can successfully do a save and this is great!!!! Works for me. And in the new object return, I can clearly see the new roleId is returned to me!!! All the other fields to RoleEntity, like the description, that is there.

But, what I ALSO want is for the User and Award fields of the RoleEntity to be fully populated as if I had done a select after the insert. Is this even possible?

I would prefer to not do a select to the user and award tables to get those objects and then assign them to this object, but if I have to do that, then that's fine.

If I need to provide any more information, please let me know. Thanks for any help in advance.

解决方案

Try this approach:

sess.save(role);
sess.flush(); //force the SQL INSERT
sess.refresh(role);

这篇关于插入与子对象的Hibernate不返回完整的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆