我打算做一个音乐站点,Message 类是存放用户留言, User类存用户信息, 数据库现在建了2涨表,Message和User的关系是 多对一,也就是一个用户可以有多条留言,但是好像在做PO和数据库映射的时候出现了,问题,大家帮我看看,谢谢。
Message.hbm.xml: <hibernate-mapping package="edu.jlu.fuliang.domain">
<class name="Message" table="message" schema="music">
<id name="id" type="java.lang.Long">
<column name="MESSAGE_ID"/>
<generator class="identity"/>
</id>
<property name="messagecontent" type="java.lang.String">
<column name="MESSAGE_CONTENT" not-null="true"/>
</property>
<property name="messagedate" type="java.lang.String">
<column name="MESSAGE_DATE" not-null="true"/>
</property>
<many-to-one name="user" class="User" column="USER_ID" lazy="false"/>
</class>
</hibernate-mapping>User.hbm.xml:<hibernate-mapping>
<class name="edu.jlu.fuliang.domain.User" table="USER">
<id name="id" type="java.lang.Long">
<column name="USER_ID"/>
<generator class="identity"/>
</id>
<property name="userName" type="java.lang.String">
<column name="USER_NAME" not-null="true"></column>
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" not-null="true"/>
</property>
<set name="messages" table="message" inverse="true" cascade="save-update">
<key column="USER_ID"/>
<one-to-many class="Message"/>
</set> </class>
</hibernate-mapping>applicationContext.xml:<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost/music">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>edu/jlu/fuliang/domain/User.hbm.xml</value>
<value>edu/jlu/fuliang/domain/Message.hbm.xml</value>
</list>
</property>
</bean>
</beans>
Message.hbm.xml: <hibernate-mapping package="edu.jlu.fuliang.domain">
<class name="Message" table="message" schema="music">
<id name="id" type="java.lang.Long">
<column name="MESSAGE_ID"/>
<generator class="identity"/>
</id>
<property name="messagecontent" type="java.lang.String">
<column name="MESSAGE_CONTENT" not-null="true"/>
</property>
<property name="messagedate" type="java.lang.String">
<column name="MESSAGE_DATE" not-null="true"/>
</property>
<many-to-one name="user" class="User" column="USER_ID" lazy="false"/>
</class>
</hibernate-mapping>User.hbm.xml:<hibernate-mapping>
<class name="edu.jlu.fuliang.domain.User" table="USER">
<id name="id" type="java.lang.Long">
<column name="USER_ID"/>
<generator class="identity"/>
</id>
<property name="userName" type="java.lang.String">
<column name="USER_NAME" not-null="true"></column>
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" not-null="true"/>
</property>
<set name="messages" table="message" inverse="true" cascade="save-update">
<key column="USER_ID"/>
<one-to-many class="Message"/>
</set> </class>
</hibernate-mapping>applicationContext.xml:<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost/music">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>edu/jlu/fuliang/domain/User.hbm.xml</value>
<value>edu/jlu/fuliang/domain/Message.hbm.xml</value>
</list>
</property>
</bean>
</beans>
解决方案 »
- java实现类似百度文库功能 调用flashpaper实现doc等格式文档转换为swf,并存到指定路径
- 搞ssh框架的
- java根据一个字段内容查询musql数据库中的信息 但是如果这个字段包含中文的话 就查询不了 该怎么办(该字段必须包含中文)
- 急!!!公司的面试题。
- 急,连接池配置
- JOSSO出错了、、、
- 我要做一个像www.800buy.com这样的网站,兄弟们进来给点意见
- 求救:form.submit()方法不存在
- jquery的message插件实现的右下角弹窗,如何记录点击了消息内容
- 关于BufferedImage的createGraphics方法在linux不执行的问题
- Struts1.2下测试action时不能加载spring配置文件
- 定义了一个拦截器 注册的时候老提示这个:
private long id;
private String messagecontent;
private String messagedate;
private User user;
private Song song; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getMessagecontent() {
return messagecontent;
} public void setMessagecontent(String messagecontent) {
this.messagecontent = messagecontent;
} public String getMessagedate() {
return messagedate;
} public void setMessagedate(String messagedate) {
this.messagedate = messagedate;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public Song getSong() {
return song;
} public void setSong(Song song) {
this.song = song;
}
}
User类:package edu.jlu.fuliang.domain;import java.util.HashSet;
import java.util.Set;public class User {
private Long id;
private String userName;
private String password;
private Set<Message>messages= new HashSet<Message>();
public Set<Message> getMessages() {
return messages;
} public void setMessages(Set<Message> messages) {
this.messages = messages;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}}
<key column="USER_ID"/>
<one-to-many class="Message"/>
</set> 中的 class 好像要写全路径哦
改为: class="edu.jlu.fuliang.domain.Message" 就好了,哈哈