简体   繁体   中英

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. I made my hibernate configuration file as

    <?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. 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

    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 . I've done some researches about this and it I got up with that I must create persistence.xml file or use spring tech.

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). You could either do that with annotation or using hbm.xml approach. In your User class, I didn't see any hibernate annotation. I therefore guess you wanna do it with hbm.xml. You need add something like

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

to your hibernate.cfg.xml.

And of course you have to create the User.hbm.xml as well. do some research on that, how to do hibernate mappings.

Personally I suggest that you do hibernate mapping with annotations. There are tons of resources about that on net.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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