- 浏览: 272779 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zhuzhuhenzhencheng:
密码是什么啊
Ext表格(Grid)上面的悬浮提示 -
鹿惊_:
确实如雪中送炭般温暖!
Ext扩展整理后吐血奉献 -
ortega1_2_3:
该版本貌似有bug,当sockIOPool的自平衡线程self ...
Java MemCached Window简单实现 -
q6952592:
好。解决了我的兼容模式下出现的问题。
Ext表格(Grid)上面的悬浮提示 -
fei33423:
请参考 fei33423的文章 java中直接调用groovy ...
Groovy应用(Java与Groovy间相互调用)
Hibernate Annotation 联合主键有三种写法 :
第一种:
Jlee01.java代码:
package com.jlee03.compositeId; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; /** * @author JLee * @since 2011-2-10 */ @Entity @Table(name="JLEE01") public class Jlee01 implements Serializable{ private static final long serialVersionUID = 3524215936351012384L; private String address ; private int age ; private String email ; private String phone ; private JleeKey01 jleeKey ; /** * @return the jleeKey */ @Id public JleeKey01 getJleeKey() { return jleeKey; } /** * @param jleeKey the jleeKey to set */ public void setJleeKey(JleeKey01 jleeKey) { this.jleeKey = jleeKey; } /** * @return the phone */ @Column(name="phone" , length=20) public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; } /** * @return the address */ @Column(name="address" , length=50) public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the age */ @Column(name="age") public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the email */ @Column(name="email" , length=23) public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } }
JleeKey01.java代码:
package com.jlee03.compositeId; import java.io.Serializable; import javax.persistence.Embeddable; /** * @author JLee * @since 2011-2-10 */ @Embeddable public class JleeKey01 implements Serializable{ private static final long serialVersionUID = -3304319243957837925L; private long id ; private String name ; /** * @return the id */ public long getId() { return id; } /** * @param id the id to set */ public void setId(long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if(o instanceof JleeKey01){ JleeKey01 key = (JleeKey01)o ; if(this.id == key.getId() && this.name.equals(key.getName())){ return true ; } } return false ; } @Override public int hashCode() { return this.name.hashCode(); } }
第二种写法:
Jlee02.java代码:
package com.jlee03.compositeId; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; /** * @author JLee * @since 2011-2-10 */ @Entity @Table(name="JLEE02") public class Jlee02 { private String address ; private int age ; private String email ; private String phone ; private JleeKey02 jleeKey ; /** * @return the jleeKey */ @EmbeddedId public JleeKey02 getJleeKey() { return jleeKey; } /** * @param jleeKey the jleeKey to set */ public void setJleeKey(JleeKey02 jleeKey) { this.jleeKey = jleeKey; } /** * @return the phone */ @Column(name="phone" , length=20) public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; } /** * @return the address */ @Column(name="address" , length=50) public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the age */ @Column(name="age") public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the email */ @Column(name="email" , length=23) public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } }
JleeKey02.java代码:
package com.jlee03.compositeId; import java.io.Serializable; /** * @author JLee */ public class JleeKey02 implements Serializable{ private static final long serialVersionUID = -3236523319933461469L; private long id ; private String name ; /** * @return the id */ public long getId() { return id; } /** * @param id the id to set */ public void setId(long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if(o instanceof JleeKey02){ JleeKey02 key = (JleeKey02)o ; if(this.id == key.getId() && this.name.equals(key.getName())){ return true ; } } return false ; } @Override public int hashCode() { return this.name.hashCode(); } }
第三种写法:
Jlee03.java代码:
package com.jlee03.compositeId; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @Entity @Table(name="JLEE03") @IdClass(JleeKey03.class) public class Jlee03 { private long id ; private String name ; /** * @return the id */ @Id public long getId() { return id; } /** * @param id the id to set */ public void setId(long id) { this.id = id; } /** * @return the name */ @Id public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } private String address ; private int age ; private String email ; private String phone ; /** * @return the phone */ @Column(name="phone" , length=20) public String getPhone() { return phone; } /** * @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; } /** * @return the address */ @Column(name="address" , length=50) public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the age */ @Column(name="age") public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the email */ @Column(name="email" , length=23) public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } }
JleeKey03.java代码:
package com.jlee03.compositeId; import java.io.Serializable; /** * @author JLee */ public class JleeKey03 implements Serializable{ private static final long serialVersionUID = 6060166117433738173L; private long id ; private String name ; /** * @return the id */ public long getId() { return id; } /** * @param id the id to set */ public void setId(long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if(o instanceof JleeKey03){ JleeKey03 key = (JleeKey03)o ; if(this.id == key.getId() && this.name.equals(key.getName())){ return true ; } } return false ; } @Override public int hashCode() { return this.name.hashCode(); } }
联合主键必须重写 equals 和 hashCode 方法。
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"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">100</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</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> <!-- Drop and re-create the database schema on startup --> <!--<property name="hbm2ddl.auto">create</property>--> <mapping class="com.jlee03.compositeId.Jlee01"/> <mapping class="com.jlee03.compositeId.Jlee02"/> <mapping class="com.jlee03.compositeId.Jlee03"/> </session-factory> </hibernate-configuration>
JleeTest.java代码:
package com.jlee03.compositeId; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class JleeTest extends TestCase { public void testJlee(){ SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory() ; Session session = sf.getCurrentSession() ; session.beginTransaction() ; Jlee01 jlee01 = new Jlee01() ; JleeKey01 key01 = new JleeKey01() ; key01.setId(0) ; key01.setName("jlee") ; jlee01.setJleeKey(key01) ; jlee01.setAddress("北京") ; jlee01.setAge(23) ; jlee01.setPhone("21321312321") ; Jlee02 jlee02 = new Jlee02() ; JleeKey02 key02 = new JleeKey02() ; key02.setId(0) ; key02.setName("jlee") ; jlee02.setJleeKey(key02) ; jlee02.setAddress("上海") ; jlee02.setAge(32) ; jlee02.setEmail("444823046@qq.com") ; Jlee03 jlee03 = new Jlee03() ; jlee03.setId(1) ; jlee03.setName("jlee") ; jlee03.setAddress("这里") ; jlee03.setAge(32) ; jlee03.setEmail("444823046@qq.com") ; session.save(jlee01) ; session.save(jlee02) ; session.save(jlee03) ; session.getTransaction().commit() ; } public void testExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ; } }
发表评论
-
关于Hibernate动态添加与修改
2011-02-18 21:39 1240定义实体 package com.jlee06.QL;imp ... -
搭建Hibernate Annotation工程
2011-02-17 00:29 1383首先从Hibernate官方网站 ... -
Hibernate对JPA的扩展
2011-02-17 00:21 4631Hibernate 3.1 提供了多种 ... -
数据库分页语句
2011-02-11 14:43 773Oracle 数据库分页 三层嵌套: select ... -
Hibernate 使用 Annotation 7(缓存的使用)
2011-02-11 09:03 1213Category.java代码: package com.j ... -
Hibernate 使用 Annotation 6(各种查询语句)
2011-02-11 08:59 2153Category.java代码: package com.j ... -
Hibernate 使用 Annotation 5(一对多双向关联)
2011-02-11 08:54 1190Group.java代码: package com.jlee ... -
Hibernate 使用 Annotation 4(一对多单向关联)
2011-02-11 08:50 1688Group.java代码: package com.jlee ... -
Hibernate 使用 Annotation 4(多对一单向关联)
2011-02-11 00:10 1616Group.java代码: package com.jlee ... -
Hibernate 使用 Annotation 2(主键生成方式)
2011-02-10 23:47 974hibernate.cfg.xml配置文件: <?xm ... -
Hibernate 注解概述
2011-02-10 23:34 1717Hibernate 注解 定义在 cla ... -
Hibernate 使用 Annotation 1(测试)
2011-02-10 23:33 786Hello World 程序 hibernate.cf ... -
HIbernate中SQLServer2000与2005的配置
2010-11-16 18:50 951<?xml version='1.0' encoding ... -
Hibernate细粒度划分与复合主键
2010-11-12 22:55 759细粒度划分 Person.hbm.xml配置文件 < ...
相关推荐
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.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. 集合...
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. 集合...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
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. ...
使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
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. ...
以上所有实体使用 JOINED 策略 Ferry和Boat class使用同名的主键关联(eg: Boat.id = Ferry.id), AmericaCupClass 和 Boat 关联的条件为 Boat.id = AmericaCupClass.BOAT_ID. 从父类继承的属性 @MappedSuperclass ...
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. ...
使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用...
《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用...
2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...
FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上...
其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...