本人想写一个用户留言板的项目,用的框架是struts+hibernate。web层已经搭建好了,现在是持久层的搭建。在本人的登录模块中,有两张表。User表和Message表。他们的关系如下:
public class Message implements Serializable { private static final long serialVersionUID = 1L; private Integer msgId; private Date msgTime; private String content; private User sender; private User getter; public Integer getMsgId() { return msgId; } public void setMsgId(Integer msgId) { this.msgId = msgId; } public Date getMsgTime() { return msgTime; } public void setMsgTime(Date msgTime) { this.msgTime = msgTime; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public User getSender() { return sender; } public void setSender(User sender) { this.sender = sender; } public User getGetter() { return getter; } public void setGetter(User getter) { this.getter = getter; } }
[code]
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer userId;
private String userpwd;
private String name;
//这里一个用户可以发送多个消息,也可以接收多个消息. one-to-many
private Set<Message> sendMessages;
private Set<Message> getMessages;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Message> getSendMessages() {
return sendMessages;
}
public void setSendMessages(Set<Message> sendMessages) {
this.sendMessages = sendMessages;
}
public Set<Message> getGetMessages() {
return getMessages;
}
public void setGetMessages(Set<Message> getMessages) {
this.getMessages = getMessages;
}
}
[/code]
其中user表和message表是:一对多的关系,主要通过user表的sendMessages和getMessaggs,外键指向Message来建立两者的关系。
他们的映射文件如下:
[code]
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping package=”com.shizongger.domain”>
<class lazy=”true” name=”User” table=”user”>
<id name=”userId” type=”java.lang.Integer”>
<generator class=”sequence”>
<param name=”sequence”>user_seq</param>
</generator>
</id>
<property name=”userpwd” type=”java.lang.String”>
<column name=”userpwd” length=”32″ />
</property>
<property name=”name” type=”java.lang.String”>
<column name=”name” length=”32″ />
</property>
<!– 配置one2many –>
<set name=”sendMessages”>
<key column=”senderId”/>
<one-to-many class=”com.shizongger.domain.Message”/>
</set>
<set name=”getMessages”>
<key column=”getterId”/>
<one-to-many class=”com.shizongger.domain.Message”/>
</set>
</class>
</hibernate-mapping>
[/code]
[code]
<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping package=”com.shizongger.domain”>
<class lazy=”true” name=”Message” table=”message”>
<!– 配置主键增长策略 –>
<id name=”msgId” type=”java.lang.Integer”>
<generator class=”sequence”>
<param name=”sequence”>msg_seq</param>
</generator>
</id>
<!– 配置普通属性 –>
<property name=”msgTime” type=”java.util.Date”>
<column name=”msgTime” />
</property>
<property name=”content” type=”java.lang.String”>
<column name=”content” length=”256″/>
</property>
<!– 配置多对一关系 –>
<many-to-one name=”sender” column=”senderId” />
<many-to-one name=”getter” column=”getterId” />
</class>
</hibernate-mapping>
[/code]
本人的main方法只是创建了一个session而已,没有任何逻辑操作,控制台输出如下:
[code]
2223 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate – updating schema
27247 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata – table found: SCOTT.MESSAGE
27247 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata – columns: [senderid, content, msgid, getterid, msgtime]
27247 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata – foreign keys: []
27247 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata – indexes: [sys_c0012468]
27257 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata – table not found: user
27268 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata – table not found: user
27278 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – Unsuccessful: create table user (userId number(10,0) not null, userpwd varchar2(32), name varchar2(32), primary key (userId))
27278 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – ORA-00903: 表名无效
27281 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – Unsuccessful: alter table message add constraint FK38EB0007A2F25B6 foreign key (senderId) references user
27281 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – ORA-00903: 表名无效
27284 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – Unsuccessful: alter table message add constraint FK38EB000736BF970C foreign key (getterId) references user
27284 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate – ORA-00903: 表名无效
27284 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate – schema update complete
[/code]
其中,msg_seq和user_seq这两个序列的创建史正确的,Message表创建也是正确的。就是没有User表的出现。
10
20
20
你数据库中的表名是user吗?或说你数据库名配错了~那个数据库中没有这张表