note:
(3)curd 增删改查 (4)在映射po类里面,有set的为一的一方。 (5)Address.hbm.xml <!-- 多对一的关键配置 --> <many-to-one name="man" class="dao.po.Man" fetch="select"> <column name="manid"/> </many-to-one> Man.hbm.xml <!-- 一对多的关键配置 --> <!-- 一般在"一"的这一方设置inverse="false" 并且只能在set一方 来设置。加上cascade 这样可以级联操作 --> <set name="addresses" inverse="false" cascade="all"> <key> <column name="manid" /> </key> <one-to-many class="dao.po.Address" /> </set> (6)在级联增加时候,先添加一的一方,否则会多出不少update的sql语句。 (7)如果级联删除就纯删除一的一方,在多的一方会出现脏数据。 (8)如果在级联更新时候,不将一的一方传入,则会在之后hibernate生成 的update语句中将多的一方外键值设为null. (9)如果在session关闭之后要用级联数据,需要在session关闭之前调用 Hibernate.initialize(...)来放内存中先。
=
spring and hiberante
(1)
private static void find()
{
//下面如果直接获取address会出现session关闭,懒加载的
问题
//但是如果用下面的方法,又觉得代码敲太多了。
List<Man> manList = hibernateTemplate.find("from
Man");
Man man = manList.get(0);
List<Address> addressList =
hibernateTemplate.findByNamedParam("from Address address where manid
= :manid", "manid", man.getId());
System.out.println(addressList.size());
//这种方法不好的地方在于调用了session,会出现session
关闭的问题。
Session session =
hibernateTemplate.getSessionFactory().openSession();
Query query = session.createQuery("from Man");
List<Man> manList = query.list();
System.out.println(manList.get(0).getAddresses
().size());
session.close();
}
=
(2)spring配置文件里面的show_sql写法。 <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property>
code:
package com.test.go;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.test.Address;
import com.test.Man;
import com.test.util.MyHibernateTemplate;
public class OntoMany extends MyHibernateTemplate
{
public static void main(String[] args)
{
// add();
// delete(26);
// update(35);
find();
}
private static void add()
{
Man man = new Man();
man.setName("tianjun");
Address temp1 = new Address();
temp1.setAddressname("123");
temp1.setMan(man);
Address temp2 = new Address();
temp2.setAddressname("3333");
temp2.setMan(man);
Address temp3 = new Address();
temp3.setAddressname("fa");
temp3.setMan(man);
Address temp4 = new Address();
temp4.setAddressname("h");
temp4.setMan(man);
hibernateTemplate.saveOrUpdate(man);
hibernateTemplate.saveOrUpdate(temp1);
hibernateTemplate.saveOrUpdate(temp2);
hibernateTemplate.saveOrUpdate(temp3);
}
private static void delete(int id)
{
Man man = new Man();
man.setId(id);
hibernateTemplate.delete(man);
}
private static void update(int id)
{
Address temp = new Address();
temp.setId(id);
temp.setAddressname("gga");
Man man = new Man();
man.setId(27);
temp.setMan(man);
hibernateTemplate.saveOrUpdate(temp);
}
private static void find()
{
//下面如果直接获取address会出现session关闭,懒加载的问题
//但是如果用下面的方法,又觉得代码敲太多了。
// List<Man> manList = hibernateTemplate.find("from Man");
// Man man = manList.get(0);
// List<Address> addressList = hibernateTemplate.findByNamedParam("from Address address where manid = :manid", "manid", man.getId());
// System.out.println(addressList.size());
//这种方法不好的地方在于调用了session,会出现session关闭的问题。
// Session session = hibernateTemplate.getSessionFactory().openSession();
// Query query = session.createQuery("from Man");
// List<Man> manList = query.list();
// System.out.println(manList.get(0).getAddresses().size());
// session.close();
}
}
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="SQLCOOL" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"> </property> <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=test"> </property> <property name="username" value="sa"></property> <property name="password" value="130727"></property> </bean> <!-- 配置session工厂 --> <bean id="CoolSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="SQLCOOL" /> </property> <property name="mappingResources"> <list> <value>com/test/T1.hbm.xml</value> <value>com/test/T2.hbm.xml</value> <value>com/test/Boy.hbm.xml</value> <value>com/test/Host.hbm.xml</value> <value>com/test/Man.hbm.xml</value> <value>com/test/Address.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> <!-- 配置Hibernate模板类 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="CoolSessionFactory" /> </property> <property name="allowCreate"> <value>true</value> </property> </bean> </beans>