如何使用Hibernate映射这些java类?

时间:2012-04-28 12:01:49

标签: java hibernate

public enum GameType{Ludo,Chess,Trivial,Monopoly,Checkers}

public class Ranking {
    private GameType game;
    private int wonGames;
    private int lostGames;
}

public class User {
    private String username;
    private String password;
    private String email;
    private String name;
    private String lastName;
    private String secondLastName;
    private ArrayList<Ranking> rankings;
}

我有这个java类,我想用Hibernate和xml映射文件映射它们,但我在排名方面遇到了一些麻烦。我如何映射这些clasess?这就是我试过的:

 <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 26-feb-2012 15:10:29 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="ProductLine.User" table="USER">
            <id name="username" type="java.lang.String">
                <column name="USERNAME" />
                <generator class="assigned" />
            </id>
            <property generated="never" lazy="false" name="password"
                type="java.lang.String">
                <column name="PASSWORD" />
            </property>
            <property generated="never" lazy="false" name="email"
                type="java.lang.String">
                <column name="EMAIL" />
            </property>
            <property column="ROLE" generated="never" lazy="false" name="role"
                not-null="true">
                <type name="org.hibernate.type.EnumType">
                    <param name="enumClass">ProductLine.RoleType</param>
                    <param name="type">12</param>
                </type>
            </property>
            <property generated="never" lazy="false" name="name"
                type="java.lang.String">
                <column name="NAME" />
            </property>
            <property generated="never" lazy="false" name="lastName"
                type="java.lang.String">
                <column name="LASTNAME" />
            </property>
            <property generated="never" lazy="false" name="secondLastName"
                type="java.lang.String">
                <column name="LAST_NAME" />
            </property>
            <set name="rankings" sort="unsorted" table="RANKINGS">
                <key column="username" />
                <many-to-many class="ProductLine.Ranking" column="game" unique="true" />
            </set>
        </class>
    </hibernate-mapping>




    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 24-abr-2012 18:37:50 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="ProductLine.Ranking" table="RANKINGS">
            <id column="game" name="id">
                <type name="org.hibernate.type.EnumType">
                    <param name="enumClass">ProductLine.GameType</param>
                    <param name="type">12</param>
                </type>
                <generator class="assigned" />
            </id>
            <property generated="never" lazy="false" name="wonGames"
                type="int">
                <column name="WONGAMES" />
            </property>
            <property generated="never" lazy="false" name="lostGames"
                type="int">
                <column name="LOSTGAMES" />
            </property>
        </class>
    </hibernate-mapping>

除排名列表外,所有地图都是正确的,这是我的问题。

由于

0 个答案:

没有答案