hibernate----1-1-----兩表關聯屬性放在另一個表裡面

来源:http://www.cnblogs.com/tk55/archive/2016/10/16/5968185.html
-Advertisement-
Play Games

、 十月 16, 2016 11:11:12 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>INFO: HCANN000001: Hibernate Commons Annotati ...


package com.ij34.dao;

import javax.persistence.*;

@Entity
@Table(name="Address_inf")
public class Address{
    @Id @Column(name="address_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int addressId;
    private String message;
    @OneToOne(targetEntity=People.class)
    
    @JoinTable(name="people_address"
    ,joinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" ,unique=true)
    ,inverseJoinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id" ,unique=true)
    )
    private People people;
    public int getAddressId() {
        return addressId;
    }
    public void setAddressId(int addressId) {
        this.addressId = addressId;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public People getPeople() {
        return people;
    }
    public void setPeople(People people) {
        this.people = people;
    }
    
}

 

 

package com.ij34.dao;

import javax.persistence.*;

@Entity
@Table(name="people_inf")
public class People implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	@Id @Column(name="people_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	private String name;
	private int age;
	@OneToOne(targetEntity=Address.class)
	@JoinTable(name="people_address"
	,joinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id" ,unique=true)
	,inverseJoinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" ,unique=true)
	)
	private Address address;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
}

  

 

 

 

package com.ij34.web;


    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.*;

import com.ij34.dao.Address;
import com.ij34.dao.People;
    public class test01 {
    public static void main(String[] args)throws Exception {
    //實例化Configuration
    Configuration conf=new Configuration().configure();
    ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    // 以Configuration實例創建SessionFactory實例
    SessionFactory SF=conf.buildSessionFactory(SR);
    //create session
    Session session=SF.openSession();
    //start 事務
    Transaction tx=session.beginTransaction();
    People person = new People();
    person.setAge(29);
    // 為複合主鍵的兩個成員設置值
   People people=new People();
   people.setAge(22);
   people.setName("林彪");
   Address a=new Address();
   a.setMessage("廣州");
   a.setPeople(people);
   session.persist(a);
   session.save(people);
    tx.commit();
    session.close();
    SF.close();
    }
    }

 

 

 

十月 16, 2016 11:11:12 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
十月 16, 2016 11:11:12 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十月 16, 2016 11:11:13 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sun Oct 16 23:11:13 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
十月 16, 2016 11:11:13 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
十月 16, 2016 11:11:13 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十月 16, 2016 11:11:13 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.address_inf
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [address_id, message]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.people_address
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [peopleid, addressid]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk_16h1wo9lfnwknsfcywssb636a, fk_aue0yqtcmxoo9hxc2cax44jg4]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk_16h1wo9lfnwknsfcywssb636a, primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.people_inf
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [people_id, name, age]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into Address_inf (message) values (?)
Hibernate: insert into people_inf (age, name) values (?, ?)
Hibernate: insert into people_address (peopleId, addressId) values (?, ?)
十月 16, 2016 11:11:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 問題1:缺少報表設計工具——即rdlc無法打開設計器 原因:缺少SQL Server Data Tools(SSDT)工具 解決:安裝ssdt即可 SSDT下載地址:https://msdn.microsoft.com/library/mt204009.aspx 參考(英):http://stack... ...
  • ...
  • **** *************************************************************************** 十月 17, 2016 1:48:19 上午 org.hibernate.annotations.common.reflection.ja ...
  • Java泛型中有存在一種方式叫做類型擦除,也就是說泛型在編譯期間進行類型檢驗上做到有效安全,但是在運行當中,會將該泛型類型用頂層父類(若無繼承關係則用Object)代替,然後再進行強轉換成目標類型,這種類型擦除也存在在泛型方法中,但是方法的擦除帶來了兩個複雜的問題。 在類型擦除之後,代碼演變成如下的 ...
  • ...
  • Python3 數字(Number) 定義:a=1 特性: 1.只能存放一個值 2.一經定義,不可更改 3.直接訪問 分類:整型,長整型,布爾,浮點,複數 python2.*與python3.*關於整型的區別 Python 數字數據類型用於存儲數值。 數據類型是不允許改變的,這就意味著如果改變數字數 ...
  • 隨筆簡介 spring版本:4.3.2.RELEASE+spring security 版本:4.1.2.RELEASE(其它不做說明) 所展示內容全部用註解配置 springmvc已經配置好,不作說明 會涉及到springmvc,spel,el的東西,不熟悉的同學可以先去看一下這方面內容,特別是s ...
  • 1.簡介 通常在R中從來進行分析和展現的數據都是以基本的格式保存的,如.csv或者.Rdata,然後使用.Rmd文件來進行分析的呈現。通過這個方式,分析師不僅可以呈現他們的統計分析的結果,還可以直接生成pdf和html文件,節省了大量的時間。但是,當你想要給其他人參閱你的文檔的時候,你就需要編譯.R ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...