Hibernate主键一对一关联映射实例【xml和注解版本@OneToOne@JoinColumn(name="wifeId")--主键单向】(十二)
来源:互联网 发布:软件测试上升空间 编辑:程序博客网 时间:2024/05/16 09:48
1 两个表Iuser Icard主键一对一映射
package com.sm.hibernate.pojo;public class IUser {private int id;private String name;private ICard iCard;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;}public ICard getiCard() {return iCard;}public void setiCard(ICard iCard) {this.iCard = iCard;}}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.sm.hibernate.pojo"><class name="IUser" table="iuser"><id name="id"><generator class="native"></generator></id><property name="name" not-null ="true"></property><one-to-one name="iCard"></one-to-one> </class></hibernate-mapping>
package com.sm.hibernate.pojo;public class ICard {private int id;private String name;private IUser iUser;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;}public IUser getiUser() {return iUser;}public void setiUser(IUser iUser) {this.iUser = iUser;}}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.sm.hibernate.pojo"><class name="ICard" table="icard"><id name="id"><generator class="foreign"><param name="property">iUser</param></generator></id><property name="name" not-null ="true"></property><one-to-one name="iUser" constrained="true"></one-to-one> </class></hibernate-mapping>
2 主配置文件添加两个引用
<mapping resource="com/sm/hibernate/pojo/ICard.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/Category.hbm.xml"/>
3 执行
Configuration cfg = new Configuration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true,true);
one-to-one name="iUser" constrained="true"></one-to-one>---此句是受约束的,这个字段
九月 27, 2013 9:24:30 上午 org.hibernate.annotations.common.Version <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}九月 27, 2013 9:24:30 上午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.1.1}九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Book.hbm.xml九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/IUser.hbm.xml九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICard.hbm.xml九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Category.hbm.xml九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null九月 27, 2013 9:24:30 上午 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect九月 27, 2013 9:24:31 上午 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000227: Running hbm2ddl schema export九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 20九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/wanju]九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000046: Connection properties: {user=root, password=****} alter table book drop foreign key FK2E3AE949F733E alter table icard drop foreign key FK5F61EF9DFFCD6A8 drop table if exists book drop table if exists category drop table if exists icard drop table if exists iuser create table book ( id integer not null auto_increment, name varchar(200) not null, author varchar(50) not null, categoryId integer, primary key (id) ) create table category ( id integer not null auto_increment, name varchar(200) not null, primary key (id) ) create table icard ( id integer not null, name varchar(255) not null, primary key (id) ) create table iuser ( id integer not null auto_increment, name varchar(255) not null, primary key (id) ) alter table book add index FK2E3AE949F733E (categoryId), add constraint FK2E3AE949F733E foreign key (categoryId) references category (id) alter table icard add index FK5F61EF9DFFCD6A8 (id), add constraint FK5F61EF9DFFCD6A8 foreign key (id) references iuser (id)九月 27, 2013 9:24:32 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stopINFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/wanju]九月 27, 2013 9:24:32 上午 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000230: Schema export complete
5 此处附完整的hibernate主配置文件
<?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">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/wanju</property><property name="connection.username">root</property><property name="connection.password">root</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="show_sql">true</property> <!-- JDBC connection pool (use the built-in) <property name="connection.pool_size">1</property> <property name="current_session_context_class">thread</property> <property name="cache.use_query_cache">true</property> <property name="cache.use_second_level_cache">true</property> <property name="cache.use_structured_entries">true</property> <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property> <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property> <property name="show_sql">true</property> <mapping resource="hibernate-config/domain/Event.hbm.xml"/> <mapping resource="hibernate-config/domain/Person.hbm.xml"/> <mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/> <mapping resource="hibernate-config/domain/Account.hbm.xml"/> <mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/> <mapping resource="hibernate-config/domain/Item.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/Course.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/Usser.hbm.xml"/>--><mapping resource="com/sm/hibernate/pojo/Book.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/IUser.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/ICard.hbm.xml"/><mapping resource="com/sm/hibernate/pojo/Category.hbm.xml"/> </session-factory></hibernate-configuration>
4
注解版实例
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;@Entitypublic class Husband {private int id;private String name;private Wife wife;@Id@GeneratedValuepublic int getId() {return id;}public String getName() {return name;}@OneToOne@JoinColumn(name="wifeId")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.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Wife {private int id;private String name;@Id@GeneratedValuepublic 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主键一对一关联映射实例【xml和注解版本@OneToOne@JoinColumn(name="wifeId")--主键单向】(十二)
- Hibernate映射(一)------单向一对一主键关联
- Hibernate一对一主键关联映射(单向)
- hibernate双向一对一主键关联映射XML与注解版
- Hibernate XML一对一主键关联 单向+双向
- 单向一对一主键关联映射
- Hibernate一对一关联(共享主键) 注解 || xml
- Hibernate 一对一主键单向关联
- Hibernate 一对一主键单向关联
- Hibernate 一对一主键单向关联
- Hibernate:一对一单向主键关联
- Hibernate 一对一主键单向关联
- 单向一对一主键关联实例
- 单向一对一主键关联实例
- Hibernate学习总结:OneToOne单向主键关联
- Hibernate关系映射(3)_一对一单向主键关联
- Hibernate关系映射(5)_一对一单向联合主键关联
- hibernate映射关系之一对一单向主键关联
- oracle用户解锁
- HDOJ_杭电2085_核反应堆,简单递归题目
- C指针——函数指针
- 小白鼠问题
- Java 项目中常见问题汇总
- Hibernate主键一对一关联映射实例【xml和注解版本@OneToOne@JoinColumn(name="wifeId")--主键单向】(十二)
- SQL性能优化
- 关于数据库添加‘时间’的Timestamp的格式处理
- 正则表达式-元字符
- 【Linux基础】QTE应用程序交叉编译实验
- 解决20%的问题,调优效果就可以达到80%
- 创建一个简单的单向链表并将其倒序输出
- 正则表达式-转义字符
- Firefox启动太慢?用speedyfox!