0

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

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

    }

这是我的使用我的用户类的类

    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 技术。

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

4

1 回答 1

2

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

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

到你的 hibernate.cfg.xml。

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

我个人建议您使用注释进行休眠映射。网上有很多关于这方面的资源。

祝你好运!

于 2012-11-27T19:03:05.497 回答