Hibernate学习笔记3.2(Hibernate组建映射)

1.组建映射

可以存在一个表里面

Husband.java

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

Wife.java

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

HibernateORMappingTest.java

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>

如果两个对象有属性一致 可以更改属性名 或者使用注解更改 映射的字段名

多对多和一对多

在多的一方加外键

1.annotation

Group.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
    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;
    }
}

User.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
    private int id;
    private String name;
    private Group group;
    @ManyToOne
    
    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
    @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;
    }
}

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">bjsxt</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.Group"/>
        <mapping class="com.bjsxt.hibernate.User"/>
 <!--  
<mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/> 
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
 -->
</session-factory> </hibernate-configuration>

test.java

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

2.xml

Group.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.Group" table="t_group">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        
    </class>
    
</hibernate-mapping>

User.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.User" table="t_user">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        <many-to-one name="group" column="groupId" />
    </class>
    
</hibernate-mapping>

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">bjsxt</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.Group"/>
            <mapping class="com.bjsxt.hibernate.User"/>
            
             -->
             <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
             <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
            
        </session-factory>
    
    </hibernate-configuration>

test.java一致

 一对多的单向关联

在多的方向加外键

Group.java

package com.bjsxt.hibernate;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
    private int id;
    private String name;
    private Set<User> users = new HashSet<User>();
    @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;
    }
    @OneToMany
    @JoinColumn(name="groupId")
    public Set<User> getUsers() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

如果不加@JoinColumv 变会生成多对多的关系

User.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
    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;
    }
}

测试

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

2  xml

Group.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.Group" table="t_group">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        <set name="users">
            <key column="groupId"></key>
            <one-to-many class="com.bjsxt.hibernate.User"/>
        </set>
    </class>
    
</hibernate-mapping>

User.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.User" table="t_user">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        
    </class>
    
</hibernate-mapping>

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">bjsxt</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.Group"/>
        <mapping class="com.bjsxt.hibernate.User"/>
        
         -->
        <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
         <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
        
    </session-factory>

</hibernate-configuration>

一对多双向

Group.java

package com.bjsxt.hibernate;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
    private int id;
    private String name;
    private Set<User> users = new HashSet<User>();
    @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;
    }
    @OneToMany(mappedBy="group")
    
    public Set<User> getUsers() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

User.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
    private int id;
    private String name;
    private Group group;
    @ManyToOne
    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
    @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;
    }
}

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">bjsxt</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.Group"/>
        <mapping class="com.bjsxt.hibernate.User"/>
        
        <!--  
        <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
         <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
         -->
    </session-factory>

</hibernate-configuration>

HibernateORMappingTest.java

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

2.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.Group" table="t_group">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        <set name="users">
            <key column="groupId"></key>
            <one-to-many class="com.bjsxt.hibernate.User"/>
        </set>
    </class>
    
</hibernate-mapping>

User.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.User" table="t_user">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        <many-to-one name="group" column="groupId"></many-to-one>
    </class>
    
</hibernate-mapping>

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">bjsxt</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.Group"/>
        <mapping class="com.bjsxt.hibernate.User"/>
         -->
        
        <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
         <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
        
    </session-factory>

</hibernate-configuration>

TEST一致

原文地址:https://www.cnblogs.com/frankzone/p/9448951.html