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

 }

}


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。