hibernate_OneToMany_CURD_demo_and_note

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>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章