Hibernate 容器 为什么我的插不进数据库东西
我用的是win7 + myEclipse7.0
代码是
public class Test {
public static void main(String args[]){
// Query query = HibernateSessionFactory.getSession().createQuery("from Users");
// List list = query.list();
// Iterator it = list.iterator();
// while(it.hasNext()){
// System.out.print("aaaa");
// }
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Users user1 = new Users();
user1.setEmail(new HashSet());
user1.setName("cht");
user1.addAddress("dddddd@ddd");
user1.addAddress("111111111@ddd");
Users user2 = new Users();
user2.setEmail(new HashSet());
user2.setName("zls");
user2.addAddress("aaaaaa@ddd");
user2.addAddress("2222222@ddd");
System.out.print("aaaa");
tx.commit();
}
}
控制台能输出aa 数据库中没东西
users.hbm.xml
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.cstp.pojo.Users" table="USERS" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="10" scale="0" />
<generator class="sequence" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" />
</property>
<set name="email" table="email">
<key column="id"/>
<element type="java.lang.String" column="address"/>
</set>
</class>
</hibernate-mapping>
其他东西是自动生成的 小弟是才学 很多都不懂 各位给指点下
------解决方案--------------------
你的user1和user2还不是持久化对象。。所以必须要相应的session.save(user1)一下。。。