- 浏览: 644350 次
- 性别:
- 来自: 常州
文章分类
- 全部博客 (345)
- java (63)
- Struts2 (11)
- Spring2.5 + (17)
- Hibernate (25)
- Struts2 Spring hibernate (5)
- log4j (3)
- apache tomcat (12)
- oracle (22)
- oracle_存储过程 (4)
- mysql (18)
- jquery (11)
- prototype (5)
- js (19)
- quartz (5)
- 设计模式 (6)
- eclipse/MyEclipse 注意事项 (9)
- eclipse (0)
- css (6)
- 正则表达式 (2)
- linux (18)
- PHP (6)
- 多线程 (20)
- XML (1)
- jstl (3)
- mongoDB (7)
- android (20)
- 反射 (1)
- IOS (46)
- SVN (3)
- C/C++ (4)
- 百度地图 (2)
- IO/SOCKET (3)
- 百度地图JS (1)
- 树莓派/香蕉派 (1)
最新评论
-
anny101:
想转发一下,不知道怎么转发。评论一下吧。方便查看。
fetch = FetchType.EAGER 作用 -
Navee:
果然我这也是是防火墙问题
解决 Linux 安装 httpd局域网无法访问 -
dhyang909:
...
oracle 10g+ 行列转换 -
国产希特勒:
真强,居然有人把公司的面试题挂到javaeye上了
锦江国际的一道面试题(很简单) -
tomfish88:
比如我要拦截不同业务的service类里面的方法 @Poi ...
Spring AOP annotation 拦截表达式 分析
Hibernate annotation 联合主键
package com.hibernate.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Husband implements Serializable { /** * */ private static final long serialVersionUID = 2476364405175138004L; private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @OneToOne public Wife getWife() { return wife; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setWife(Wife wife) { this.wife = wife; } }
package com.hibernate.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; @Entity @IdClass(WifePK.class)//这种设置主键的方式最简单(在下面联合主键的键上设@Id) public class Wife implements Serializable { /** * */ private static final long serialVersionUID = 5270130768836331730L; private int id; private String name; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } @Id public String getName() { return name; } public void setName(String name) { this.name = name; } }
Wife的主键类
package com.hibernate.entity; import java.io.Serializable; public class WifePK implements Serializable { /** * */ private static final long serialVersionUID = 5270130768836331730L; private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
hibernate.cfg.xml
<?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="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost/test </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="com.hibernate.entity.Husband" /> <mapping class="com.hibernate.entity.Wife" /> </session-factory> </hibernate-configuration>
然后用junit4调试:
package com.hibernate.test; import org.hibernate.SessionFactory; public class ORMappingTest { private static SessionFactory sessionFactory; @BeforeClass public static void setUpBeforeClass() throws Exception { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void tearDownAfterClass() throws Exception { sessionFactory.close(); } @Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create(true, true); } }
生成的SQL文日志:
create table Husband ( id integer not null auto_increment, name varchar(255), wife_id integer, wife_name varchar(255), primary key (id) ) create table Wife ( id integer not null, name varchar(255) not null, primary key (id, name) ) alter table Husband add index FKAEEA401B2DB1BA15 (wife_id, wife_name), add constraint FKAEEA401B2DB1BA15 foreign key (wife_id, wife_name) references Wife (id, name)
发表评论
-
OneToMany字段序列化 failed to lazily initialize a collection of role
2014-06-06 18:51 1452hibernate项目中,如果要对onetomany的po ... -
hibernate 本地查询 字段别名 映射到 DTO时注意事项
2014-04-22 13:54 3499本地原生SQL: SELECT pole.pId as ... -
实体中的数据库字段有关键字
2014-03-26 15:08 978mysql中如果表的字段设成了关键字,那么在做某些操作时会抛 ... -
Spring +hibernate 声明式 事物 + annotation
2012-03-22 19:49 907在applicationContext.xml中 &l ... -
Hibernate distinct
2012-02-15 18:05 1064/** * 得到大区编号和名称 * @ret ... -
@Transactional 事务回滚 分析
2011-11-08 16:51 4276@Transactional 事务回滚 Spring的 ... -
Hibernate 性能优化_3
2011-10-25 15:54 605二级缓存 对于二级缓存,其实并不一定要在项目中使用 ... -
Hibernate 性能优化_2
2011-10-20 17:51 822createQuery("FROM ****&quo ... -
Hibernate 性能优化_1
2011-10-18 18:39 962大概如此:不一定说在每个项目中都合适 1、 使用se ... -
Hibernate 的 Criteria用法,完整的一个例子
2011-10-18 16:49 1105数据库: /* MySQL Data Transfer ... -
转的:Hibernate 的 Criteria用法
2011-10-18 16:37 1298方法 说明 Restric ... -
Hibernate 的 join
2011-10-18 13:29 973转的: 1.如果没有在Hibernate配置里做关 ... -
fetch = FetchType.EAGER 作用
2011-10-13 11:47 28697如果是EAGER,那么表示取出这条数据时,它关联的数据也同时取 ... -
用myeclipse的Hibernate 反向引擎 生成 数据库的 entity
2011-09-22 14:44 8798把 Myeclipse 转到DB Browser 新建 ... -
Hibernate annotation 多对多双向关联(很少用)
2011-09-21 16:44 1670双向关联 在认为的 主表 上用到 @JoinTable ... -
Hibernate annotation 多对多单向关联
2011-09-21 16:33 1576package com.hibernate.entity ... -
Hibernate annotation 一对多,多对一
2011-09-21 16:03 1934package com.hibernate.entity ... -
Hibernate annotation 的各种关系
2011-09-15 15:59 989转的: 一、@OneToOne Java ... -
Hibernate 双向关联
2011-09-15 15:55 969不管是啥关联,只要是双向,都必须用到mappedBy ... -
OneToOne annotation 双向关联
2011-09-15 15:51 1156package com.hibernate.entity ...
相关推荐
NULL 博文链接:https://cdxs2.iteye.com/blog/1932507
第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 第4课 资源 7 第5课 环境准备 7 ...三、 联合主键 24 1、xml方式 24 2、annotation方式 27 第14课 Hibernate核心开发接口(重点) 29 ........
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...
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 ...
触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...
触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. ...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接...
10.5 联合军种作战的混乱 10.5.1 Spring事务管理器的应对 10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接...
从内存中清除,从账户中取出amt,如果amt>账户余额抛出异常,一个实体Bean可以表示不同的数据实例,我们应该通过主键来判断删除哪个数据实例…… ejbCreate函数用于初始化一个EJB实例 5个目标文件,演示Address ...
保证Bean正常被激活和钝化,EJB对象是用完毕,从内存中清除,从账户中取出amt,如果amt>账户余额抛出异常,一个实体Bean可以表示不同的数据实例,我们应该通过主键来判断删除哪个数据实例…… ejbCreate函数用于初始...
(读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看...