Hibernate註解多對一關聯關係

來源:互聯網
上載者:User

標籤:hibernate工具類   多對一關聯性   註解   hibernate   

實體類1

package entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Dream{
 private int did;
 private String ddesc;
 private Person person;
 @ManyToOne
 @JoinColumn
 public Person getPerson() {
  return person;
 }
 public void setPerson(Person person) {
  this.person = person;
 }
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 public int getDid() {
  return did;
 }
 public void setDid(int did) {
  this.did = did;
 }
 public String getDdesc() {
  return ddesc;
 }
 public void setDdesc(String ddesc) {
  this.ddesc = ddesc;
 }
 
}

實體類2

package entity;


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

@Entity
public class Person{
 private int pid;
 private String name;
 
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 public int getPid() {
  return pid;
 }
 public void setPid(int pid) {
  this.pid = pid;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
}

 

設定檔:

<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
       
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
       
         <!-- SQL dialect 資料庫方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
       
         <!-- JDBC connection pool (use the built-in)串連池 -->
        <property name="connection.pool_size">2</property>

      
         <!-- Enable Hibernate‘s current session context   當前session-->
        <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>

        <!-- Disable the second-level cache   -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout  在控制台顯示sql語句-->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup
        java代碼自動產生資料庫裡面的表
        create 每次都會根據java代碼,建立表,刪除上次的表
        update 【常用】 在上一次的基礎上修改資訊
        create——drop 根據類產生表
        validate 不建立表,但是會插入新值 -->     
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <!--   將實體類中的設定檔引入hibernate中,聲明設定檔 -->
      
        <mapping  class="entity.Person"></mapping>
       <mapping class="entity.Dream"></mapping>
    </session-factory>

</hibernate-configuration>

 

測試類別:

package test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;

import entity.Dream;
import entity.Person;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Person p=new Person();
  p.setName("韓迎賓");
  
  Dream d=new Dream(); 
  d.setDdesc("5年後寫一個Oracle");
  d.setPerson(p);
  Session session=HibernateUtil.getCurrentSession();
  Transaction transaction=session.beginTransaction();
  session.save(p);
  session.save(d);
  transaction.commit();

 }

}


 

Hibernate註解多對一關聯關係

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.