做了个一对多的例子,一个Quetion对应多个Answer,以下是代码:
测试的代码:
public static void main(String[] args) throws Exception {
ApplicationContext ctx = null;
ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); QuestionDAO qdao = (QuestionDAO) ctx.getBean("QuestionDAO");   
Question q = new Question();
q.setUserid("a");
q.setTitle("a");
q.setSubid(3);
q.setStatus(2);
q.setQuestiontime(new Date());
q.setOfferscore(5);
q.setItemid(1);
q.setGrade("嗯");
q.setContent("好");
q.setCommenflag(1);
q.setClickcount(4);
q.setAcceptflag(1); Answer ans = new Answer();
ans.setAnstime(new Date());
ans.setGrade("嗯");
ans.setUserid("a");
ans.setStatus(3);
ans.setQuesans("我");
ans.setQuestion(q);
q.getAnswers().add(ans);
qdao.insert(q);
}
}ApplicationContext.xml:
<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:3306/bbs"></property>
<property name="username" value="root"></property>
<property name="password" value="angpy19870101"></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="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 为单向关联(一对一, 多对一)的外连接抓取(outer join fetch)树设置最大深度. 值为0意味着将关闭默认的外连接抓取 -->
                <prop key="hibernate.max_fetch_depth">3</prop>
                <!-- 为Hibernate关联的批量抓取设置默认数量 -->
                <prop key="hibernate.default_batch_fetch_size">8</prop>
                <!-- 强制Hibernate按照被更新数据的主键,为SQL更新排序。这么做将减少在高并发系统中事务的死锁。 -->
                <prop key="hibernate.order_updates">true</prop>
                <!-- session在事务完成后将被自动清洗(flush) -->
                <prop key="hibernate.transaction.flush_before_completion">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/angpy/bbs/dao/User.hbm.xml</value>
<value>cn/angpy/bbs/dao/Item.hbm.xml</value>
<value>cn/angpy/bbs/dao/Subitem.hbm.xml</value>
<value>cn/angpy/bbs/dao/Question.hbm.xml</value>
<value>cn/angpy/bbs/dao/Answer.hbm.xml</value>
<value>cn/angpy/bbs/dao/Admin.hbm.xml</value>
</list>
</property>
</bean>
<bean id="QuestionDAO" class="cn.angpy.bbs.dao.QuestionDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="AnswerDAO" class="cn.angpy.bbs.dao.AnswerDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>Question.hbm.xml:
<hibernate-mapping>
<class name="cn.angpy.bbs.dao.Question" table="question"
catalog="bbs">
<id name="qid" type="java.lang.Integer">
<column name="qid" />
<generator class="increment" />
</id>
<set name="answers" inverse="true" cascade="all" lazy="false">
<key>
<column name="qid" />
</key>
<one-to-many class="cn.angpy.bbs.dao.Answer" />
</set>
</class>
</hibernate-mapping>
Answer.hbm.xml:
<hibernate-mapping>
<class name="cn.angpy.bbs.dao.Answer" table="answer"
catalog="bbs">
<id name="aid" type="java.lang.Integer">
<column name="aid" />
<generator class="increment" />
</id>
<many-to-one name="question" class="cn.angpy.bbs.dao.Question"
fetch="select">
<column name="qid" />
</many-to-one>
</hibernate-mapping>
为什么执行insert时HQL语句只有SELECT??没有insert啊???数据库插不入数据???