我在我的 java 项目中使用 hibernate 时遇到问题,希望您能帮助我:
首先,这是我的一些代码:
实体类
package de.zaed.entities;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="USER")
public class User {
/**
* the user id in the database
*/
@Id
private Long id;
/**
* the username of a user
*/
private String username;
/**
* the users password
*/
private String password;
/**
* The role of a user
*/
@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
private UserRole role;
public Long getId() {
return id;
}
public String getPassword() {
return password;
}
public UserRole getRole() {
return role;
}
public String getUsername() {
return username;
}
public void setId(Long id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setRole(UserRole role) {
this.role = role;
}
public void setUsername(String username) {
this.username = username;
}
}
我将类添加到 AnnotationConfiguration 的部分
config.addAnnotatedClass(User.class);
config.addAnnotatedClass(UserRole.class);
config.configure("hibernate.cfg.xml");
现在我的问题:
在执行以下代码时:
User dbUser = new User();
dbUser.setUsername(username);
dbUser.setPassword(password);
Session session = DatabaseService.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(dbUser);
session.flush();
session.close();
我明白了
org.hibernate.MappingException: Unknown entity: de.zaed.entities.User
调用 session.save(dbUser) 时出错。
我在论坛和一些教程中阅读了很多帖子,但我找不到解决我的问题的方法。希望您能够帮助我。
谢谢, kaQn4p