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 logVersion
INFO: 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 buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
九月 27, 2013 9:24:30 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: 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 addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Book.hbm.xml
九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: 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 addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/IUser.hbm.xml
九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: 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 addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICard.hbm.xml
九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: 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 addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Category.hbm.xml
九月 27, 2013 9:24:30 上午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: 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 doConfigure
INFO: 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 execute
INFO: HHH000227: Running hbm2ddl schema export
九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
九月 27, 2013 9:24:31 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: 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 configure
INFO: 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 stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/wanju]
九月 27, 2013 9:24:32 上午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: 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;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
public 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;
@Entity
public class Wife {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}