- 浏览: 92159 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (133)
- jQuery (11)
- XML (3)
- 组件 (1)
- JAVA (20)
- WEB (3)
- SPRING (6)
- HIBERNATE (5)
- AJAX (2)
- JS (1)
- JAVA webservice (1)
- Ditu (1)
- WEBSITE (1)
- HIBERNATE ANNOTATION (1)
- 排序 (1)
- TCP_NODELAY (1)
- ConvertUtils (1)
- Logistics (1)
- SQL SERVER 中identity (4)
- sql server (35)
- MYSQL (1)
- Eclipse (6)
- ORACLE (6)
- FLEX (4)
- notepad++ (0)
- UNION ALL (1)
- JUnit (3)
- SQL 异常处理 (1)
- @@trancount (1)
- IOS (1)
- ORA-02266 (1)
- REMOTE DESKTOP (0)
- HTML 优化 (1)
- CRLF (1)
- SQL Server Sequence (1)
最新评论
-
zjuttsw:
看的舒服
重要的hashcode equals转载
转载:http://yugouai.iteye.com/blog/974170
关键字: hibernate级联配置
1.级联是指两个对象之间的操作联动关系,对一个对象执行了操作之后,对其指定的级联对象也需要执行相同的操作;
2.级联配置通过many-to-one的cascade属性实现;
3.cascade属性有四种取值:
all:所有操作都执行级联操作;
none:所有操作都不执行级联操作;
save-update:保存和更新时执行级联操作;
delete:删除时执行级联操作;
4.通过级联操作可以解决持久化对象不能直接引用瞬时对象的问题。
hibernate.cfg.xml:
Java代码
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">useName</property>
<property name="connection.url">url</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">...</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">driveclass</property>
<property name="show_sql">true</property>
<mapping resource="pojo/Group.hbm.xml" />
<mapping resource="pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
Group.java:
Java代码
package pojo;
public class Group {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User.java:
Java代码
package pojo;
public class User {
private long id;
private String username;
private String password;
private Group group;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Group.hbm.xml:
Java代码
<?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="pojo.Group" table="GROUP" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>
User.hbm.xml:
Java代码
<?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="pojo.User" table="USER" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="10" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="10" not-null="true" />
</property>
<many-to-one name="group" column="RELATEDGROUP" cascade="all" />
</class>
</hibernate-mapping>
HibernateSessionFactory.java:
Java代码
package factory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.out
.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
ExportToDB.java:
Java代码
package export;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportToDB {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
ExportDBAddCascade.java:
Java代码
package export;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import factory.HibernateSessionFactory;
import pojo.Group;
import pojo.User;
public class ExportDBAddCascade {
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
Group group = new Group();
group.setName("J2EE Developer");
for (int i = 0; i < 10; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("111");
user.setGroup(group);
session.save(user);
}
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
关键字: hibernate级联配置
1.级联是指两个对象之间的操作联动关系,对一个对象执行了操作之后,对其指定的级联对象也需要执行相同的操作;
2.级联配置通过many-to-one的cascade属性实现;
3.cascade属性有四种取值:
all:所有操作都执行级联操作;
none:所有操作都不执行级联操作;
save-update:保存和更新时执行级联操作;
delete:删除时执行级联操作;
4.通过级联操作可以解决持久化对象不能直接引用瞬时对象的问题。
hibernate.cfg.xml:
Java代码
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">useName</property>
<property name="connection.url">url</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="myeclipse.connection.profile">...</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">driveclass</property>
<property name="show_sql">true</property>
<mapping resource="pojo/Group.hbm.xml" />
<mapping resource="pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
Group.java:
Java代码
package pojo;
public class Group {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User.java:
Java代码
package pojo;
public class User {
private long id;
private String username;
private String password;
private Group group;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Group.hbm.xml:
Java代码
<?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="pojo.Group" table="GROUP" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>
User.hbm.xml:
Java代码
<?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="pojo.User" table="USER" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="10" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="10" not-null="true" />
</property>
<many-to-one name="group" column="RELATEDGROUP" cascade="all" />
</class>
</hibernate-mapping>
HibernateSessionFactory.java:
Java代码
package factory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.out
.println("Error Creating SessionFactory");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
ExportToDB.java:
Java代码
package export;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportToDB {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
ExportDBAddCascade.java:
Java代码
package export;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import factory.HibernateSessionFactory;
import pojo.Group;
import pojo.User;
public class ExportDBAddCascade {
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
Group group = new Group();
group.setName("J2EE Developer");
for (int i = 0; i < 10; i++) {
User user = new User();
user.setUsername("name" + i);
user.setPassword("111");
user.setGroup(group);
session.save(user);
}
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
发表评论
-
hibernate 乐观锁与悲观锁使用
2013-03-03 19:50 567http://blog.csdn.net/yufaw/arti ... -
Spring Aop 拦截 Struts2 Action 有关问题
2013-03-03 11:21 573http://www.myexception.cn/softw ... -
hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)等
2013-03-01 13:36 504http://www.blogjava.net/dreamst ... -
Hibernate session的方法全集
2013-02-26 15:28 520http://www.cnblogs.com/lukun/ar ...
相关推荐
Hibernate级联操作一对多demo Hibernate级联操作一对多demo Hibernate级联操作一对多demo
Hibernate的级联操作(增,删,该)
Hibernate级联操作.docx
hibernate 的基础 常用的主键生成策略 持久化对象的三种状态 一级缓存的常用操作(save/update/saveOrUpdate/delete)一对多关联映射 级联操作
用Struts2+Spring+Hibernate三大框架完成的级联操作,包括省市县和年月日
Hibernate 与数据库中的触发器协同工作时, 会造成两类问题 1、触发器使 Session 的缓存中的持久化对象与数据库中对应的数据不一致:触发器运行在数据库中, 它执行的操作对 Session 是透明的 Session 的 解决...
struts2 hibernate 实现无限级联 树形分类 基于struts2 hibernate 技术,实现对无限级联分类的crud操作
这个小项目使用jsf+richfaces+hiberante设计,作为学习jsf和hibernate的新手学习参考.里面由基本上有常规项目所需一般功能:比如ajax.分页.hibernate级联操作等等
struts2 spring jpa操作数据库 级联数据 hibernate
Hibernate级联操作对象的关系属性: 映射文件中设置: 1. Cascade a) none b) Save-update 当使用session的api对当前对象进行save,update操作时,对它的关系属性也进行save或者update c) Delete d) All = delete ...
10_传智播客JPA详解_JPA中的一对多双向关联与级联操作.rar
详解Hibernate cascade级联属性的CascadeType的用法 cascade(级联) ...cascade表示级联操作,在hibernate配置注解@OneToOne,@OneToMany,@ManyToMany,@ManyToOne中的属性。 例如: @ManyToOne(c
Struts2+Hibernate写的下拉菜单级联两级分类,不带JRA包,自己下载吧,
Hibernate 练习题 关于一些级联操作的练习题 映射
该项目为一个整合hibernate与struts,利用json对象存放临时数据库数据,连接数据的三级...Struts.xml:主要亮点在配置一个多变级联时防止加载多个表的操作,从而达到解决多表级联操作的时候经常出现的session已关闭的bug
基于Struts 2+Hibernate实现员工管理系统。一个单位有多个部门,每个部门有多名员工,每个员工只属于一...基于该要求,实现员工和部门的管理(包括添加、查询、删除和修改等操作),并且要实现部门与员工之间的级联操作。
详细讲解了hibernate 的多对一xml配置以及curd操作,讲解了级联操作,适合初学者学习
本人在厦门邦初培训时候用的ppt 快速入门文档 内容: 建立一对多的关联关系域模型 建立一对多的参照关系的模型 映射一对多关联关系 通过hibernateAPI级联操作关联对象 其它关系的映射:一对一,多对多
在上一篇的项目基础上,添加了多对多的级联操作,和操作中间表