简体   繁体   English

使用MySQL的Hibernate类中的未知实体错误

[英]Unknown Entity error in Hibernate class with MySQL

I'm working on a project and this is first time that I'm using hibernate tech. 我正在开发一个项目,这是我第一次使用hibernate技术。 I made my hibernate configuration file as 我把我的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>

And this I've done my reverse engineering configuration so I get my MySQL tables as Java classes. 而且我已经完成了逆向工程配置,因此我将MySQL表作为Java类。 This is my User Class; 这是我的用户类;

    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;
}

    }

This are my classes that uses the my Users class 这是我使用我的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;
    }

and

public boolean RegisterUsers(Users user) {

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

}

At least this is my test class. 至少这是我的测试课。

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);

}
}

When I execute my test class I got this error; 当我执行我的测试类时,我得到了这个错误;

Unknown entity: Tables.Users . 未知实体:Tables.Users。 I've done some researches about this and it I got up with that I must create persistence.xml file or use spring tech. 我已经做了一些关于这方面的研究,我得知我必须创建persistence.xml文件或使用spring技术。

Sorry for long post, I'm waiting for any helps. 对不起,很长的帖子,我在等待任何帮助。 Thanks. 谢谢。

you need to let Hibernate know that which class would be mapped to persistent layer(database table for example). 你需要让Hibernate知道哪个类将映射到持久层(例如数据库表)。 You could either do that with annotation or using hbm.xml approach. 您可以使用注释或使用hbm.xml方法执行此操作。 In your User class, I didn't see any hibernate annotation. 在您的User类中,我没有看到任何hibernate注释。 I therefore guess you wanna do it with hbm.xml. 因此,我猜你想用hbm.xml做。 You need add something like 你需要添加类似的东西

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

to your hibernate.cfg.xml. 到你的hibernate.cfg.xml。

And of course you have to create the User.hbm.xml as well. 当然,您还必须创建User.hbm.xml。 do some research on that, how to do hibernate mappings. 做一些研究,如何做hibernate映射。

Personally I suggest that you do hibernate mapping with annotations. 我个人建议您使用注释进行休眠映射。 There are tons of resources about that on net. 网上有大量的资源。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM