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>
分享到:
相关推荐
JPA_OneToMany学习教程JPA_OneToMany学习教程JPA_OneToMany学习教程JPA_OneToMany学习教程
C#例子代码 A0622_EF_OneToManyC#例子...A0622_EF_OneToManyC#例子代码 A0622_EF_OneToManyC#例子代码 A0622_EF_OneToManyC#例子代码 A0622_EF_OneToManyC#例子代码 A0622_EF_OneToManyC#例子代码 A0622_EF_OneToMany
spring_ioc_onetomany
Hibernate Annotation几种关联映射 一对一(One-To-One) 使用@OneToOne注解建立实体Bean之间的一对一关联。一对一关联有三种情况:(1).关联的实体都共享同样的主键,(2).其中一个实体通过外键关联到另一个实体的主键...
博文链接:https://llying.iteye.com/blog/220809
NULL 博文链接:https://tgwall163.iteye.com/blog/1277750
EJB基本知识介绍。如OneToOne,OneToMany等经典实例推荐
基于HIBERNATE 操作表的关系练习例子
一对多关系的 Hibernate 注释
onetomany-hibernate在myeclipse下的使用开发hibernate程序
在 Hibernate 中,CascadeType属性通常用于@OneToMany、@ManyToOne和@ManyToMany注解中,用于定义实体之间的级联操作。例如,在订单系统中,订单和订单项之间的关系可以使用@OneToMany注解,并设置CascadeType为ALL...
“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”“#跟踪狐狸”
hibernate对象三状态及OneToOne&OneToMany&ManyToMany讲解,代码示例,数据库示例!非常全面
Hibernate课程 在这个资料库中,我尝试学习Hibernate 话题 简单的结局 预先映射 @OneToOne @OneToMany @多多 提取类型 懒惰的 渴望的
* 9.increnment 插入数据的时候hibernate会给主键添加一个自增的主键,但是一个hibernate实例就维护一个计数器,所以在多个实例运行的时候不能使用这个方法。 * 例:@GeneratedValue(generator = ...
Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity ...
使用MySql示例的带有Hibernate的Spring Data JPA 该项目使用MySql Example描述了带有Spring Data JPA和Hibernate的Spring Boot示例。 描述 该项目显示了存储在MySql数据库中的用户列表。 使用以下端点,可以实现...
一对多Nosql演示与OneToMany关系中的MongoDB的Spring Data演示项目 该项目是我发表的文章的来源。 请以下文章的链接:
JPA课程manyToMany OneToMany 等全部测试 注解, 有注释