java - MySQL 的 Hibernate 类中出现未知实体错误

标签 java mysql hibernate

我正在开发一个项目,这是我第一次使用 hibernate 技术。 我将 hibernate 配置文件设置为

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">diobookbla</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mobil</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.default_schema">mobil</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        </session-factory>
    </hibernate-configuration>

我已经完成了逆向工程配置,因此我将 MySQL 表作为 Java 类获取。 这是我的用户类;

    package Tables;
    // default package
    // Generated Nov 24, 2012 1:08:44 AM by Hibernate Tools 3.4.0.CR1

    import java.util.HashSet;
    import java.util.Set;

    /**
     * Users generated by hbm2java
     */
    public class Users implements java.io.Serializable {

private Integer userId;
private Integer userFbId;
private String userFbToken;
private String userName;
private String userLastName;
private String userEmail;
private String userAdress;
private String userFavouritedTopicsCount;
private String userCreatedTopicsCount;
private String userRecommendedTopicsCount;
private Integer userFollowingsCount;
private Integer userFollowersCount;
private String userBio;
private String userProfilePicUrl;
private Boolean userIsOnline;
private Integer userPoint;
private Set topicses = new HashSet(0);
private Set mentionsForMentionedId = new HashSet(0);
private Set favouritetopicses = new HashSet(0);
private Set mentionsForMentionerId = new HashSet(0);
private Set postlikes = new HashSet(0);
private Set userfollowsForFollowingId = new HashSet(0);
private Set topicrecommends = new HashSet(0);
private Set userfollowsForFollowerId = new HashSet(0);

public Users() {
}

public Users(Integer userFbId, String userFbToken, String userName,
        String userLastName, String userEmail, String userAdress,
        String userFavouritedTopicsCount, String userCreatedTopicsCount,
        String userRecommendedTopicsCount, Integer userFollowingsCount,
        Integer userFollowersCount, String userBio,
        String userProfilePicUrl, Boolean userIsOnline, Integer userPoint,
        Set topicses, Set mentionsForMentionedId, Set favouritetopicses,
        Set mentionsForMentionerId, Set postlikes,
        Set userfollowsForFollowingId, Set topicrecommends,
        Set userfollowsForFollowerId) {
    this.userFbId = userFbId;
    this.userFbToken = userFbToken;
    this.userName = userName;
    this.userLastName = userLastName;
    this.userEmail = userEmail;
    this.userAdress = userAdress;
    this.userFavouritedTopicsCount = userFavouritedTopicsCount;
    this.userCreatedTopicsCount = userCreatedTopicsCount;
    this.userRecommendedTopicsCount = userRecommendedTopicsCount;
    this.userFollowingsCount = userFollowingsCount;
    this.userFollowersCount = userFollowersCount;
    this.userBio = userBio;
    this.userProfilePicUrl = userProfilePicUrl;
    this.userIsOnline = userIsOnline;
    this.userPoint = userPoint;
    this.topicses = topicses;
    this.mentionsForMentionedId = mentionsForMentionedId;
    this.favouritetopicses = favouritetopicses;
    this.mentionsForMentionerId = mentionsForMentionerId;
    this.postlikes = postlikes;
    this.userfollowsForFollowingId = userfollowsForFollowingId;
    this.topicrecommends = topicrecommends;
    this.userfollowsForFollowerId = userfollowsForFollowerId;
}

public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public Integer getUserFbId() {
    return this.userFbId;
}

public void setUserFbId(Integer userFbId) {
    this.userFbId = userFbId;
}

public String getUserFbToken() {
    return this.userFbToken;
}

public void setUserFbToken(String userFbToken) {
    this.userFbToken = userFbToken;
}

public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getUserLastName() {
    return this.userLastName;
}

public void setUserLastName(String userLastName) {
    this.userLastName = userLastName;
}

public String getUserEmail() {
    return this.userEmail;
}

public void setUserEmail(String userEmail) {
    this.userEmail = userEmail;
}

public String getUserAdress() {
    return this.userAdress;
}

public void setUserAdress(String userAdress) {
    this.userAdress = userAdress;
}

public String getUserFavouritedTopicsCount() {
    return this.userFavouritedTopicsCount;
}

public void setUserFavouritedTopicsCount(String userFavouritedTopicsCount) {
    this.userFavouritedTopicsCount = userFavouritedTopicsCount;
}

public String getUserCreatedTopicsCount() {
    return this.userCreatedTopicsCount;
}

public void setUserCreatedTopicsCount(String userCreatedTopicsCount) {
    this.userCreatedTopicsCount = userCreatedTopicsCount;
}

public String getUserRecommendedTopicsCount() {
    return this.userRecommendedTopicsCount;
}

public void setUserRecommendedTopicsCount(String userRecommendedTopicsCount) {
    this.userRecommendedTopicsCount = userRecommendedTopicsCount;
}

public Integer getUserFollowingsCount() {
    return this.userFollowingsCount;
}

public void setUserFollowingsCount(Integer userFollowingsCount) {
    this.userFollowingsCount = userFollowingsCount;
}

public Integer getUserFollowersCount() {
    return this.userFollowersCount;
}

public void setUserFollowersCount(Integer userFollowersCount) {
    this.userFollowersCount = userFollowersCount;
}

public String getUserBio() {
    return this.userBio;
}

public void setUserBio(String userBio) {
    this.userBio = userBio;
}

public String getUserProfilePicUrl() {
    return this.userProfilePicUrl;
}

public void setUserProfilePicUrl(String userProfilePicUrl) {
    this.userProfilePicUrl = userProfilePicUrl;
}

public Boolean getUserIsOnline() {
    return this.userIsOnline;
}

public void setUserIsOnline(Boolean userIsOnline) {
    this.userIsOnline = userIsOnline;
}

public Integer getUserPoint() {
    return this.userPoint;
}

public void setUserPoint(Integer userPoint) {
    this.userPoint = userPoint;
}

public Set getTopicses() {
    return this.topicses;
}

public void setTopicses(Set topicses) {
    this.topicses = topicses;
}

public Set getMentionsForMentionedId() {
    return this.mentionsForMentionedId;
}

public void setMentionsForMentionedId(Set mentionsForMentionedId) {
    this.mentionsForMentionedId = mentionsForMentionedId;
}

public Set getFavouritetopicses() {
    return this.favouritetopicses;
}

public void setFavouritetopicses(Set favouritetopicses) {
    this.favouritetopicses = favouritetopicses;
}

public Set getMentionsForMentionerId() {
    return this.mentionsForMentionerId;
}

public void setMentionsForMentionerId(Set mentionsForMentionerId) {
    this.mentionsForMentionerId = mentionsForMentionerId;
}

public Set getPostlikes() {
    return this.postlikes;
}

public void setPostlikes(Set postlikes) {
    this.postlikes = postlikes;
}

public Set getUserfollowsForFollowingId() {
    return this.userfollowsForFollowingId;
}

public void setUserfollowsForFollowingId(Set userfollowsForFollowingId) {
    this.userfollowsForFollowingId = userfollowsForFollowingId;
}

public Set getTopicrecommends() {
    return this.topicrecommends;
}

public void setTopicrecommends(Set topicrecommends) {
    this.topicrecommends = topicrecommends;
}

public Set getUserfollowsForFollowerId() {
    return this.userfollowsForFollowerId;
}

public void setUserfollowsForFollowerId(Set userfollowsForFollowerId) {
    this.userfollowsForFollowerId = userfollowsForFollowerId;
}

    }

这是我的类,使用我的 Users 类

    public boolean SaveDatabase(Object object) {        
    try {

        SessionFactory sessionfactory = new Configuration().configure()
                .buildSessionFactory();
        session = sessionfactory.openSession();
        try {
            session.save(object);
            session.flush();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return false;
    } finally {

        session.close();
    }
    return true;
    }

public boolean RegisterUsers(Users user) {

    if (SaveDatabase(user)) {
        return true;
    } else {
        return false;
    }

}

至少这是我的测试课。

import Tables.Users;
import DAO.LogIn;

public class Test {
public static void main(String[] args) {
        //Test RegisterUser and GetUsersWithId in here

    LogIn test = new LogIn();
    Users user = new Users();

    user.setUserEmail("email");
    user.setUserAdress("adres here");
    user.setUserFbId(3);

    test.RegisterUsers(user);

}
}

当我执行测试类时,出现此错误;

未知实体:Tables.Users。 我对此做了一些研究,我发现我必须创建 persistence.xml 文件或使用 spring 技术。

抱歉,帖子很长,我正在等待任何帮助。 谢谢。

最佳答案

你需要让Hibernate知道哪个类将被映射到持久层(例如数据库表)。您可以使用注释或使用 hbm.xml 方法来做到这一点。 在您的 User 类中,我没有看到任何 hibernate 注释。因此我猜你想用 hbm.xml 来做到这一点。 您需要添加类似的内容

<mapping resource="path/to/your/User.hbm.xml"/>

到你的 hibernate.cfg.xml。

当然,您还必须创建 User.hbm.xml。对此进行一些研究,了解如何进行 hibernate 映射。

我个人建议您使用注释进行 hibernate 映射。网上有大量关于这方面的资源。

祝你好运!

关于java - MySQL 的 Hibernate 类中出现未知实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590968/

相关文章:

java - 在maven eclipse项目中找不到spring框架的标签库描述符

java - BufferedReader 中的缓冲区大小是多少?

php - 我需要html标签来做回车吗?

java - JPA:反向映射字段上的查询子句 - 未设置参数

java - com.fasterxml.jackson.databind.JsonMappingException : Multiple back-reference properties with name 'defaultReference'

java - 空指针异常

java - 在 Java 中什么是嵌套类,它们有什么作用?

java - 如何防止异常时自动递增?

sql语法中的php mysqli转义字符串错误

php - MySQL 性能规划