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 Tech を使用する必要があることに気付きました。

長い投稿で申し訳ありません、私は何か助けを待っています。ありがとう。

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 に答える