简体   繁体   English

更新多对一休眠关系无用

[英]update on many-to-one hibernate relationship no works

I have a problem with hibernate relationship many-to-one. 休眠关系多对一有问题。

My system is commercial proposal controller, where has a responsible for proposal from user entity. 我的系统是商业提案控制器,其中负责用户实体的提案。 When the proposal is create and set the responsible, has no problems, works fine. 提案创建并设置负责人时,没有问题,工作正常。 But when change the responsible and update, it changes the object, I can see in a datatable, but has no update in database. 但是当更改负责人并进行更新时,它更改了对象,我可以在数据表中看到,但是数据库中没有更新。 If I refresh on page the update disapears. 如果我在页面上刷新,更新将消失。

    @Entity
    @Table(name = "proposal")
    public class Proposal implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne
    private User responsible;
   ............      

DAO Code DAO代码

    public void update(Proposal proposal) {
    this.session.update(proposal);

    }

In the User class I don't make any annotation about this relationship, it's a unidirectional relationship. 在User类中,我没有对此关系做任何注释,它是单向关系。 The class proposal yet use the user class to make a user's bag, as participants, and this relationship will be unidirectional relationship too. 班级提议还使用用户班级作为参与者制作用户的书包,并且这种关系也将是单向关系。

I tried to make annotations in user class but no works too. 我试图在用户类中进行批注,但是也没有用。

User class with annotations 带有注释的用户类别

   @Entity
   @Table(name="user") 
   public class User implements Serializable{

    @OneToMany 
    private List<Proposal> proposal;

The class User has a many-to-one relationship with userType and it works fine. User类与userType具有多对一关系,并且可以正常工作。

    @Entity
    @Table(name="user") 
    public class User implements Serializable{

    @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "idType, nullable = true)
     private TipoUsuario    userType;

Someone has any ideia about this? 有人对此有任何想法吗?

Thanks 谢谢

You'll see changes after flushing to database. 刷新到数据库后,您将看到更改。 Either manually - flush() or after transaction has been committed. 手动-flush()或提交事务之后。 The User class - must be either an entity (both @Entity and @Id annotations are needed) or embedable class and should be configured in the corresponding way in this case. User类-必须是一个实体(需要@Entity和@Id批注)或可嵌入类,并且在这种情况下应以相应的方式进行配置。

I'm not sure how it works if you say - @Entity without @Id for User, but try again: 如果您说-@Entity没有@Id的User,我不确定它是如何工作的,但是请重试:

  1. Run flush for User. 为用户运行冲洗。 If you won't get any exception - it is a persisatble object. 如果您没有任何异常-这是一个持久对象。 You should see in db/web application log files - sql insert query. 您应该在db / web应用程序日志文件中看到-sql插入查询。
  2. Commit transaction. 提交交易。 The updated value of User should be committed. 用户的更新值应该被提交。

If you invoke refresh, before commit, all your changes will be lost. 如果调用刷新,则在提交之前,所有更改都将丢失。

It looks to me like you're missing the @JoinColumn annotation from your ManyToOne relationship with your User. 在我看来,您与用户的ManyToOne关系中缺少@JoinColumn批注。 Try updating to this: 尝试更新为此:

@ManyToOne
@JoinColumn(name="userId")
private User responsible;

But obviously replace 'userId' with the id name as its specified in your table. 但是显然,将“ userId”替换为表中指定的ID名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM