Hibernate一對一單向外鍵關聯


一、一對一單向外鍵關聯:

  一對一單向外鍵關聯主要用到了以下兩個注解:

  1、OneToOne(cascade=CasecadeTYPE.ALL);

    cascade=CasecadeTYPE.ALL:表示的是表之間的級聯關系,比如級聯刪除,級聯更新等,ALL表示的是全級聯。

 

  2、JoinColumn(name="sid" unique=true);

    JoinColumn: 表示將被控類的外鍵寫在主控類中

  主要是用到主表中。

  需要注意的點是: 在保存時應該先保存外鍵對象再保存主表對象;

  

 

問題一: 在編寫實體類Students.java的時候發生一個錯誤:

Type mismatch: cannot convert from CascadeType to CascadeType[]

檢查之后發現錯誤是導致的原因是注解注解引用的包出現了問題:

@OneToOne(cascade=CascadeType.ALL)//表示全 級聯關系
@Column(name="pid" unique=true)

導致錯誤的包:

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.metamodel.binding.CascadeType;

正確的包:

import javax.persistence.OneToOne;

 

問題一: 在一切都准備好了,要生成表結構的同時出現了第二個問題:問題的描述是這樣的:

2016-4-17 10:38:46 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2016-4-17 10:38:46 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.21.Final}
2016-4-17 10:38:46 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2016-4-17 10:38:46 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2016-4-17 10:38:46 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016-4-17 10:38:46 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016-4-17 10:38:46 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2016-4-17 10:38:46 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016-4-17 10:38:46 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016-4-17 10:38:46 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016-4-17 10:38:46 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016-4-17 10:38:46 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2016-4-17 10:38:47 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-4-17 10:38:47 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4

最主要的是這一句:

INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
貌似是我的jdbc的版本太低,所以導致測試沒有通過。但是這不是導致錯誤的主要原因,版本低一般不會導致問題出現,
有一篇博客有同樣的問題但是還是執行成功了:http://blog.csdn.net/xwin1989/article/details/7380736
最主要的原因是如下的報錯信息:


org.hibernate.AnnotationException: @Column(s) not allowed on a @OneToOne property: oto_fk.Students.card
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1745)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:895)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:728)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3625)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3579)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1381)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)
    at oto_fk.TestStudents.testShemaExport(TestStudents.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的Students.java實體類的類容是這樣的:
package oto_fk;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity; /*JPA注解*/
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;



/**
 * 學生實體類
 * @author Administrator
 *
 */
//@Entity(name="t_students")
@Entity//表示這是一個實體類
//schema:表示數據庫的名稱
//name:表示數據庫的表名
//Embedddable注解表示一個非Entity類,但是可以嵌入到另外一個實體類中作為屬性而存在
public class Students implements Serializable{

    private IdCard card;
    private int sid;  //將學號改成字符串類型
    private String gender; //性別
    private Date birthday;//出生日期
    private String major;//專業

    public Students(IdCard card,int sid, String gender, Date birthday,
            String major) {
        super();
        this.card = card;    
        this.sid = sid;
        this.gender = gender;
        this.birthday = birthday;
        this.major = major;
    }


    @OneToOne(cascade=CascadeType.ALL)//表示全 級聯關系
    @Column(name="pid", unique=true)
    public IdCard getCard() {
        return card;
    }


    public void setCard(IdCard card) {
        this.card = card;
    }


    @Id
    @GeneratedValue//主鍵自增
    public int getSid() {
        return sid;
    }


    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public Date getBirthday() {
        return birthday;
    }


    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    public String getMajor() {
        return major;
    }


    public void setMajor(String major) {
        this.major = major;
    }


    public Students()
    {
        
    }

}

導致錯誤的原因是:

 @OneToOne(cascade=CascadeType.ALL)//表示全 級聯關系
    @Column(name="pid", unique=true)
    public IdCard getCard() {
        return card;
    }

應該改成:

    @OneToOne(cascade=CascadeType.ALL)//表示全 級聯關系
    @JoinColumn(name="pid", unique=true)
    public IdCard getCard() {
        return card;
    }

 

以下將正確執行的代碼貼出:

1、 students.java實體類(主控類):

package oto_fk;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity; /*JPA注解*/
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;



/**
 * 學生實體類
 * @author Administrator
 *
 */
//@Entity(name="t_students")
@Entity//表示這是一個實體類
//schema:表示數據庫的名稱
//name:表示數據庫的表名
//Embedddable注解表示一個非Entity類,但是可以嵌入到另外一個實體類中作為屬性而存在
public class Students implements Serializable{

    private IdCard card;
    private int sid;  //將學號改成字符串類型
    private String gender; //性別
    private Date birthday;//出生日期
    private String major;//專業

    public Students(IdCard card,int sid, String gender, Date birthday,
            String major) {
        super();
        this.card = card;    
        this.sid = sid;
        this.gender = gender;
        this.birthday = birthday;
        this.major = major;
    }


    @OneToOne(cascade=CascadeType.ALL)//表示全 級聯關系
    @JoinColumn(name="pid", unique=true)
    public IdCard getCard() {
        return card;
    }


    public void setCard(IdCard card) {
        this.card = card;
    }


    @Id
    @GeneratedValue//主鍵自增
    public int getSid() {
        return sid;
    }


    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public Date getBirthday() {
        return birthday;
    }


    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    public String getMajor() {
        return major;
    }


    public void setMajor(String major) {
        this.major = major;
    }


    public Students()
    {
        
    }

}

 

2、IdCard實體類(被控類):

package oto_fk;

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

import org.hibernate.annotations.GenericGenerator;

/*身份證類*/
@Entity
public class IdCard {

    private String pid;//身份證號
    private String sname;//學生姓名
    
    //無參數的構造器
    public IdCard()
    {
        
    }

    //帶參數的構造器
    public IdCard(String pid, String sname) {
        super();
        this.pid = pid;
        this.sname = sname;
    }

    @Id//指定主鍵
    @Column(length=18)//指定身份證的長度
    //主鍵生成策略
    @GenericGenerator(name="pid",strategy="assigned")
    @GeneratedValue(generator="pid")
    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}

 

3、Hibernate.cfg.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!-- old: http://hibernate.sourceforge.net/hibernate-configuration-3.6.dtd -->
<!-- new: http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd -->
<!--    : http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd -->
<hibernate-configuration>
<session-factory>
    <!-- 顯示sql語句 -->
    <property name="show_sql">true</property>
    <property name="myeclipse.connection.profile">bookshop</property>
    <!-- <property name="connection.url">
        jdbc:mysql://localhost:3306/bookshop
        jdbc:mysql://localhost:3306/database?useUnicode=true&amp;characterEncoding=UTF-8 
    </property> -->
    
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
    <property name="connection.username">root</property>
    <property name="connection.password">woaiwojia..123</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.current_session_context_class">thread</property>
    
    <!-- 將實體類映射到數據庫 -->
    <mapping class="oto_fk.Students"/>
    <mapping class="oto_fk.IdCard"/>
    
    
</session-factory>
</hibernate-configuration>

 

4、測試用例:

 1 package oto_fk;
 2 
 3 import org.hibernate.SessionFactory;
 4 import org.hibernate.cfg.Configuration;
 5 import org.hibernate.service.ServiceRegistry;
 6 import org.hibernate.service.ServiceRegistryBuilder;
 7 import org.hibernate.tool.hbm2ddl.SchemaExport;
 8 import org.junit.Test;
 9 
10 public class TestStudents {
11 
12     @Test
13     public void testShemaExport(){
14         //創建Hibernate配置對象
15         Configuration configuration = new Configuration().configure();
16         
17         //創建服務注冊對象
18         ServiceRegistry serviceRegistry = 
19                 new ServiceRegistryBuilder()
20         .applySettings(configuration.getProperties())
21         .buildServiceRegistry();
22         
23         //創建sessionFactory
24         SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
25         
26         //生成SchemaExport對象
27         SchemaExport export = new SchemaExport(configuration);
28         //調用schemaExport的create生成數據庫表結構
29         export.create(true, true);    
30     }
31 }

 

5、數據庫表生成log:

2016-4-17 10:53:56 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2016-4-17 10:53:56 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.21.Final}
2016-4-17 10:53:56 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2016-4-17 10:53:56 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2016-4-17 10:53:56 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016-4-17 10:53:56 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016-4-17 10:53:56 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2016-4-17 10:53:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016-4-17 10:53:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016-4-17 10:53:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016-4-17 10:53:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016-4-17 10:53:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2016-4-17 10:53:56 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-4-17 10:53:56 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2016-4-17 10:53:57 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2016-4-17 10:53:57 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: IdCard
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Students
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: IdCard
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Students
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: IdCard
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Students
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
2016-4-17 10:53:57 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}

    alter table Students 
        drop 
        foreign key FK_mryi31xkbwbosevquurf60ivb

    drop table if exists IdCard

    drop table if exists Students

    create table IdCard (
        pid varchar(18) not null,
        sname varchar(255),
        primary key (pid)
    )

    create table Students (
        sid integer not null auto_increment,
        birthday datetime,
        gender varchar(255),
        major varchar(255),
        pid varchar(18),
        primary key (sid)
    )

    alter table Students 
        add constraint UK_mryi31xkbwbosevquurf60ivb unique (pid)

    alter table Students 
        add index FK_mryi31xkbwbosevquurf60ivb (pid), 
        add constraint FK_mryi31xkbwbosevquurf60ivb 
        foreign key (pid) 
        references IdCard (pid)
2016-4-17 10:53:57 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016-4-17 10:53:57 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

 

6、數據庫表生成情況:

 

 

7、編寫一個測試用例來測試一對一單向外鍵關聯的效果:

@Test
    public void addStudents()
    {
        //創建Hibernate配置對象
        Configuration configuration = new Configuration().configure();
        
        //創建服務注冊對象
        ServiceRegistry serviceRegistry = 
                new ServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .buildServiceRegistry();
        
        //創建sessionFactory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        //創建會話對象
        Session session = sessionFactory.getCurrentSession();
        //開啟事務
        Transaction tx = session.beginTransaction();
        //生成一個學生身份證對象
        IdCard card = new IdCard("888888888888888888","劉德華");
        //生成一個學生對象
        Students stu = new Students(card,"男",new Date(),"計算機");
        //先保存被控表對象(身份證)
        session.save(card);
        session.save(stu);
        //提交事務
        tx.commit();
        

 

Log:

2016-4-17 11:19:45 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2016-4-17 11:19:45 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.21.Final}
2016-4-17 11:19:45 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2016-4-17 11:19:45 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2016-4-17 11:19:45 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016-4-17 11:19:45 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016-4-17 11:19:45 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2016-4-17 11:19:45 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016-4-17 11:19:45 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016-4-17 11:19:45 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016-4-17 11:19:45 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016-4-17 11:19:45 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2016-4-17 11:19:46 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-4-17 11:19:46 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2016-4-17 11:19:46 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2016-4-17 11:19:46 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.idcard
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [pid, sname]
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.students
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [birthday, sid, gender, pid, major]
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk_mryi31xkbwbosevquurf60ivb]
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary, fk_mryi31xkbwbosevquurf60ivb, uk_mryi31xkbwbosevquurf60ivb]
2016-4-17 11:19:47 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: 
    insert 
    into
        IdCard
        (sname, pid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        Students
        (birthday, pid, gender, major) 
    values
        (?, ?, ?, ?)

 

數據庫數據插入情況:

IdCard表

 

Students表:

 

一對一的單向外鍵關聯到此結束,轉載的同仁注明出處。謝謝!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM