码迷,mamicode.com
首页 > Web开发 > 详细

hibernate组件映射

时间:2016-04-22 13:27:19      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

对象关系: 一个对象是另外一个对象的一部分

数据库表: 一张表

比如一个学生有一个学生证, 学生证有很多信息.

一个wife属于一个husband:

1. wife.java: id去掉, 不需要主键, 因为是husband的一部分

package com.bjsxt.hibernate;
public class Wife {	
	private String wifeName;
	private int age;	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}		
	public String getWifeName() {
		return wifeName;
	}
	public void setWifeName(String name) {
		this.wifeName = name;
	}	
}

2. husband.java: 加入@Embedded

package com.bjsxt.hibernate;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
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;
	}
	@Embedded
	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;
	}
	
}

 

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>

         
        <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">linda0213</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
       <!--
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
        <property name="connection.username">scott</property>
        <property name="connection.password">tiger</property>
      	<property name="dialect">org.hibernate.dialect.OracleDialect</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>

        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">update</property>
		 -->       
		<mapping class="com.bjsxt.hibernate.Husband"/>		
		<!-- <mapping resource="com/bjsxt/hibernate/Husband.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

test文件:

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateORMappingTest {
	private static SessionFactory sessionFactory;
	
	//@BeforeClass
	public static void beforeClass() {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	//@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
	
	
	
	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}
	
	public static void main(String[] args) {
		beforeClass();
	}
}

  

 

XML方式:

加入Husband.hbm.xml:

<?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>
	<class name="com.bjsxt.hibernate.Husband" >
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<component name="wife">
			<property name="wifeName"></property>
			<property name="age"></property>
		</component>
    </class>
	
</hibernate-mapping>

hibernate.cfg.xml配置:

<mapping resource="com/bjsxt/hibernate/Husband.hbm.xml"/>

  

  

  

  

 

hibernate组件映射

标签:

原文地址:http://www.cnblogs.com/wujixing/p/5420760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!