我把配置文件发出来,各位帮我看看那里错了Comment.hbm.xml文件如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="model">    <class name="Comment" table="Comments">
        <id name="commentId" column="CommentId" type="int" unsaved-value="0" >
            <generator class="identity"/>
        </id> <property name="subject" column="Subject" type="string" length="50" not-null="true"/>
<property name="body" column="Body" type="string" not-null="true"/>
<property name="postDate" column="PostDate" type="java.util.Date" not-null="true"/>
<property name="entry" column="Entry" type="int" not-null="true"/>
<property name="replyTo" column="ReplyTo" type="int" not-null="false"/>
<many-to-one name="postedBy" class="model.User" column="PostedBy">
<column name="UserId"/>
</many-to-one>
    </class></hibernate-mapping>
Entry.hbm.xml文件如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="Entry">    <class name="Entry" table="Entries">
        <id name="entryId" column="EntryId" type="int" unsaved-value="0" >
            <generator class="identity"/>
        </id> <property name="subject" column="Subject" type="string" length="50" not-null="true"/>
<property name="body" column="Body" type="string" not-null="true"/>
<property name="postDate" column="postDate" type="java.util.Date" not-null="true"/>
        <set name="comments" table="Comments">
            <key column="Entry"/>
<one-to-many class="model.Comment"/>
        </set>
    </class></hibernate-mapping>

解决方案 »

  1.   

    数据库的脚本如下:
    create table Entries (
    EntryId int not null auto_increment,

    Subject varchar(50) not null,
    Body text not null,
    PostDate datetime not null,

    constraint PK_EntryId primary key (EntryId)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;create table Comments (
    CommentId int not null auto_increment,

    Subject varchar(50) not null,
    Body text not null,
    PostDate datetime not null,
    Entry int not null,
    ReplyTo int null,
    PostedBy int null,

    index (ReplyTo),
    index (PostedBy),
    index (Entry),

    constraint PK_CommentId primary key (CommentId),
    constraint FK_ReplyTo foreign key (ReplyTo) references Comments(CommentId) on delete cascade,
    constraint FK_PostedBy foreign key (PostedBy) references Users(UserId) on delete cascade,
    constraint FK_Entry foreign key (Entry) references Entries(EntryId) on delete cascade
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      

  2.   

    <many-to-one name="postedBy" class="model.User" column="PostedBy">
    <column name="UserId"/>
    </many-to-one>
    这里不能同时使用column属性和column元素,两者取一即可。值为外键的那个列名。