gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:12:00 26 4
gpt4 key购买 nike

我遇到异常

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com