我的用户类包含字段名字,姓氏,性别,电子邮件,密码,DOB,地址和角色。
@Entity
@Table(name="User_s180")
public class User {
@Id
private String Email;
@Column(nullable=false)
private String FirstName;
private String LastName;
@Column(nullable=false)
private String Gender;
@Column(nullable=false)
private String Mobile_Number;
@Column(nullable=false)
private Date DOB;
@Column(nullable=false)
private String Address;
@Column(nullable=false)
private String Password;
@Column(nullable=false)
private String Role;
@Column(name="Online_status")
private boolean Online;
我在这里有所有领域的getter和setter
我想创建电子邮件作为我的主键,因此我有@Id但是因为它是字符串类型我不能使用@Generation注释。 每当我尝试在Db中插入一个新用户时我得到一个错误嵌套异常是org.hibernate.id.IdentifierGenerationException:在根本原因调用save():com.niit.model.User]之前必须手动分配此类的ID
Hibernate代码
package com.niit.Dao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.niit.model.User;
@Repository
@Transactional
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sf;
public UserDaoImpl()
{
System.out.println("UserDaoImpl is Instantiated");
}
public void Registration(User user) {
Session ssn=sf.getCurrentSession();
ssn.save(user);
}
public boolean isEmailUnique(String email) {
// TODO Auto-generated method stub
Session ssn=sf.getCurrentSession();
Query query=ssn.createQuery("from User where email=:email");
query.setString("email",email);
User user=(User)query.uniqueResult();
//User already exists
if(user!=null)
{
return false;
}
else
{
return true;
}
}
public User Login(User user) {
// TODO Auto-generated method stub
Session ssn=sf.getCurrentSession();
Query query=ssn.createQuery("from User where email=:email and password=:password");
query.setString("email",user.getEmail());
query.setString("password", user.getPassword());
User validuser=(User)query.uniqueResult();
return validuser;
}
public void UpdateUserOnlineStatus(User user) {
// TODO Auto-generated method stub
Session ssn=sf.getCurrentSession();
ssn.update(user);
}
}
控制器类
package com.niit.controllers;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.niit.Dao.UserDao;
import com.niit.model.Errorclass;
import com.niit.model.User;
@Controller
public class UserController {
@Autowired
private UserDao userdao;
public UserController(){
System.out.println("UserController is instantiated");
}
@RequestMapping(value="/Signupuser",method=RequestMethod.POST)
public ResponseEntity<?> Registration(@RequestBody User user){
System.out.println("Registration function invoked");
if(userdao.isEmailUnique(user.getEmail()))
{
System.out.println("Email is Unique --> Registering User");
userdao.Registration(user);
System.out.println("User Registered Successfully");
return new ResponseEntity<User>(user,HttpStatus.OK);
}
else
{
System.out.println("Email is not Unique --> User cannot be Registered");
Errorclass ec=new Errorclass(10,"Email-id already exists");
return new ResponseEntity<Errorclass>(ec,HttpStatus.CONFLICT);
}
}
答案 0 :(得分:0)
if(userdao.isEmailUnique(user.getEmail()))检查不足以确保您在请求中收到了有效的电子邮件地址。如果电子邮件为空,您的查询将转换为&#34;来自用户,其中email = null&#34;。
尝试在UserDaoImpl.Registration方法中打印User类,并确保您有一个电子邮件地址。