java - 异常 : Session Issue ids for this class must be manually assigned before calling save(): org. me.Testservices.TblUsers

标签 java hibernate exception netbeans

我遇到异常

Session Issue ids for this class must be manually assigned before calling save(): org.me.Testservices.TblUsers

package org.me.Testservices;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

/**
 * TblUsers generated by hbm2java
 */
@Entity
@Table(name="tbl_users"
    ,catalog="ceotrp"
    , uniqueConstraints = @UniqueConstraint(columnNames="Mobile_No") 
)
public class TblUsers  implements java.io.Serializable {


     private String email;
     private String username;
     private String password;
     private Date createTime;
     private String mobileNo;
     private String tblUsersType;
     private String tblUserscol;

    public TblUsers() {
    }


    public TblUsers(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.password = password;
    }
    public TblUsers(String email, String username, String password, Date createTime, String mobileNo, String tblUsersType, String tblUserscol) {
       this.email = email;
       this.username = username;
       this.password = password;
       this.createTime = createTime;
       this.mobileNo = mobileNo;
       this.tblUsersType = tblUsersType;
       this.tblUserscol = tblUserscol;
    }

     @Id 


    @Column(name="email", unique=true, nullable=false)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    @Column(name="username", nullable=false, length=16)
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Column(name="password", nullable=false, length=32)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="create_time", length=26)
    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }


    @Column(name="Mobile_No", unique=true, length=14)
    public String getMobileNo() {
        return this.mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }


    @Column(name="tbl_UsersType", length=1)
    public String getTblUsersType() {
        return this.tblUsersType;
    }

    public void setTblUsersType(String tblUsersType) {
        this.tblUsersType = tblUsersType;
    }


    @Column(name="tbl_Userscol", length=45)
    public String getTblUserscol() {
        return this.tblUserscol;
    }

    public void setTblUserscol(String tblUserscol) {
        this.tblUserscol = tblUserscol;
    }

}`

>I AM NEW TO HIBERNATE in NETBEANS 8.2 . The Select Query is Working Fine ,but INSERT is Not Happening .

>>The Code Below Demonstrates how I am using hibernate and the Above Code for Inserting in MySQL 8.0 (Which is Of course not Happening)



public String registernewUser(  String name , String email,String password,String MObileNo,String Utype)
        {
            TblUsers ins =new TblUsers();
            ins.setUsername(name);
            ins.setPassword(password);
            ins.setMobileNo(MObileNo);
            ins.setTblUsersType(Utype);
            ins.setTblUserscol("D");
            ins.setTblUsersType("C");
            try{


           /* session.beginTransaction();
            session.save(ins);
            session.getTransaction().commit();
            session.close();*/

            Transaction t= session.beginTransaction();
           session.save(ins);
           t.commit();
           return "Operation executed";     




            }
            catch(Throwable ex)
            {
                 System.err.println("Session Issue " + ex.getMessage());
                    return ex.getMessage();
              }

           }
           }

如果您需要其他文件,我可以提供。我正在尝试为 Android 应用程序创建一个 Web 服务,而且我也喜欢 Hibernate 技术。请让我知道优化上述代码的最佳方法。我正在使用 Glassfish Server 来测试 Web 服务。

最佳答案

TblUsers @Entity 类的 @Id 字段具有 String 类型,因此它无法为您生成 ids。标识符的值必须通过以下方式设置保存前的应用程序。

   public String registernewUser(  String name , String email,String password,String MObileNo,String Utype)
            {
                TblUsers ins =new TblUsers();
                ins.setUsername(name);    
                ins.setEmail(email);   // set email 
                ins.setPassword(password);
                ins.setMobileNo(MObileNo);
                ins.setTblUsersType(Utype);
                ins.setTblUserscol("D");
                ins.setTblUsersType("C");
                try{


               /* session.beginTransaction();
                session.save(ins);
                session.getTransaction().commit();
                session.close();*/

                Transaction t= session.beginTransaction();
               session.save(ins);
               t.commit();
               return "Operation executed";     




                }
                catch(Throwable ex)
                {
                     System.err.println("Session Issue " + ex.getMessage());
                        return ex.getMessage();
                  }

               }
               }

关于java - 异常 : Session Issue ids for this class must be manually assigned before calling save(): org. me.Testservices.TblUsers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60452917/

相关文章:

java - 由 : java. lang.ClassNotFoundException 引起 : org. hibernate.cfg.AnnotationConfiguration 无法被 net.sf.jasperreports_6.10.0.final 找到

hibernate - Grails:执行两个修改同一个对象的连续事务的最佳方法是什么?

java - 使用 Tomcat 9 时出现 Jasper 异常

java - 如何在Java中解析JSON

java - 如何在java web项目中配置自定义日志文件

java - 如何使用itext7.1在原始内容下添加文本水印

c++ - 抛出未处理的自定义异常后如何调用 what()?

java - struts中点击anchor标签时如何发送参数

java.lang.NoClassDefFoundError : javax/persistence/SharedCacheMode 错误

java - 从 Swing 元素更新嵌入式 JavaFX TableView 时堆栈溢出