hibernate 多对多双向关联

1、一个员工对应一张卡,因此Person与Card之间的关联是一对一关系。

Person类代码如下:

package com.mr.cheng.bean;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import org.hibernate.annotations.Cascade;

@Entity
public class Person {
	private Integer id;
	private String name;
	private int age;
	private double price;
	private Card card;
	@OneToOne
	(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="cardId")
	public Card getCard() {
		return card;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age
				+ ", price=" + price + ", card=" + card + "]";
	}
	public void setCard(Card card) {
		this.card = card;
	}
	@Id
	@GeneratedValue
	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 double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
}

  Card类的代码如下:

package com.mr.cheng.bean;

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

@Entity
public class Card {
	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;
	}
	@OneToOne(mappedBy="card")
	public Person getPerson() {
		return person;
	}
	@Override
	public String toString() {
		return "Card [id=" + id + ", name=" + name + ", person=" + person + "]";
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	private Person person;
}

 2、测试类

package com.mr.cheng.test;

import java.util.Date;
import java.util.HashSet;

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;

import com.mr.cheng.bean.Card;
import com.mr.cheng.bean.Person;


public class HibernateTest {
	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(true, true);
	}
	
	public static void main(String[] args) {
		beforeClass();
	}
	/**
	 * 一对一关联
	 */
	@Test
	public void testAddPerson(){
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		Person p = new Person();
		Card card = new Card();
		card.setName("ccc");
		p.setAge(3);
		p.setName("cheg");
		p.setCard(card);
		session.save(p);
		session.getTransaction().commit();
		
	}
	@Test
	public void testAddQueryPerson(){
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Person p = (Person) session.get(Person.class, 2);
		System.out.println(p.getCard().getName() + p.getCard().getId());
		session.getTransaction().commit();
	}
	@Test
	public void testAddQueryCard(){
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.createCriteria(Card.class).list();
		session.getTransaction().commit();
	}
	/**
	 * 
	 */
	
}

 3、hibernate.hbm.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/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</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>
<mapping class="com.mr.cheng.bean.Card" />
<mapping class="com.mr.cheng.bean.Person" />

</session-factory>

</hibernate-configuration>

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