zoukankan      html  css  js  c++  java
  • hibernate---联合主键关联

    被主导方wife有两个主键:

    package com.bjsxt.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.IdClass;
    
    @Entity
    @IdClass(WifePK.class)
    public class Wife {
    	private int id;
    	private String name;
    	private int age;
    	
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	@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;
    	}
    	
    }
    

    wifePK.java:

    package com.bjsxt.hibernate;
    
    import java.io.Serializable;
    
    public class WifePK implements Serializable {
    	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;
    	}
    	
    }
    

    主导方husband.java:

    package com.bjsxt.hibernate;
    
    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;
    	}
    	@OneToOne
    	@JoinColumns(
    		{
    			@JoinColumn(name="wifeId", referencedColumnName="id"),
    			@JoinColumn(name="wifeName", referencedColumnName="name")
    		}
    	)
    	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;
    	}
    	
    }
    

    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 class="com.bjsxt.hibernate.Wife"/>
    
        </session-factory>
    
    </hibernate-configuration>
    

    unittest:

    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();
    	}
    }
    

      

      

  • 相关阅读:
    Java 代码块
    Java 方法签名
    Java 中的继承
    Java 中的this关键字
    Java 静态对象 static
    Java报错 -- The public type c must be defined in its own file
    Java 构造方法
    Java 成员变量和局部变量
    Java 对象的创建和使用
    JavaScript单线程和异步机制
  • 原文地址:https://www.cnblogs.com/wujixing/p/5420691.html
Copyright © 2011-2022 走看看