Hibernate Annotation 基于连接表的单向一对多关联。一对多的关联形式我就不说了,这个例子是基于连接表jointable的,不懂的参数可以看下 Hibernate Annotation Reference 。
sql脚本(附件)
- – MySQL dump 10.13 Distrib 5.1.55, for Win32 (ia32)
- –
- – Host: localhost Database: hibernate_demo
- – ——————————————————
- – Server version 5.1.55-community
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8 */;
- /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
- /*!40103 SET TIME_ZONE=’+00:00′ */;
- /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=’NO_AUTO_VALUE_ON_ZERO’ */;
- /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
- –
- – Table structure for table `address`
- –
- DROP TABLE IF EXISTS `address`;
- /*!40101 SET @saved_cs_client = @@character_set_client */;
- /*!40101 SET character_set_client = utf8 */;
- CREATE TABLE `address` (
- `addressId` int(11) NOT NULL AUTO_INCREMENT,
- `item` varchar(30) NOT NULL,
- PRIMARY KEY (`addressId`)
- ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
- /*!40101 SET character_set_client = @saved_cs_client */;
- –
- – Dumping data for table `address`
- –
- LOCK TABLES `address` WRITE;
- /*!40000 ALTER TABLE `address` DISABLE KEYS */;
- INSERT INTO `address` VALUES (1,’item1′),(2,’item2′);
- /*!40000 ALTER TABLE `address` ENABLE KEYS */;
- UNLOCK TABLES;
- –
- – Table structure for table `jointable`
- –
- DROP TABLE IF EXISTS `jointable`;
- /*!40101 SET @saved_cs_client = @@character_set_client */;
- /*!40101 SET character_set_client = utf8 */;
- CREATE TABLE `jointable` (
- `pid` int(11) DEFAULT NULL,
- `aid` int(11) DEFAULT NULL,
- KEY `join_person_fk` (`pid`),
- KEY `join_address_fk` (`aid`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- /*!40101 SET character_set_client = @saved_cs_client */;
- –
- – Dumping data for table `jointable`
- –
- LOCK TABLES `jointable` WRITE;
- /*!40000 ALTER TABLE `jointable` DISABLE KEYS */;
- INSERT INTO `jointable` VALUES (1,1),(1,2);
- /*!40000 ALTER TABLE `jointable` ENABLE KEYS */;
- UNLOCK TABLES;
- –
- – Table structure for table `person`
- –
- DROP TABLE IF EXISTS `person`;
- /*!40101 SET @saved_cs_client = @@character_set_client */;
- /*!40101 SET character_set_client = utf8 */;
- CREATE TABLE `person` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(30) NOT NULL,
- `age` int(11) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
- /*!40101 SET character_set_client = @saved_cs_client */;
- –
- – Dumping data for table `person`
- –
- LOCK TABLES `person` WRITE;
- /*!40000 ALTER TABLE `person` DISABLE KEYS */;
- INSERT INTO `person` VALUES (1,’zhaoyunpeng’,10);
- /*!40000 ALTER TABLE `person` ENABLE KEYS */;
- UNLOCK TABLES;
- /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
- /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
- /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
- /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
- /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
- – Dump completed on 2012-08-17 0:17:14
-- MySQL dump 10.13 Distrib 5.1.55, for Win32 (ia32) -- -- Host: localhost Database: hibernate_demo -- ------------------------------------------------------ -- Server version 5.1.55-community /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `address` -- DROP TABLE IF EXISTS `address`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `address` ( `addressId` int(11) NOT NULL AUTO_INCREMENT, `item` varchar(30) NOT NULL, PRIMARY KEY (`addressId`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `address` -- LOCK TABLES `address` WRITE; /*!40000 ALTER TABLE `address` DISABLE KEYS */; INSERT INTO `address` VALUES (1,'item1'),(2,'item2'); /*!40000 ALTER TABLE `address` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `jointable` -- DROP TABLE IF EXISTS `jointable`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `jointable` ( `pid` int(11) DEFAULT NULL, `aid` int(11) DEFAULT NULL, KEY `join_person_fk` (`pid`), KEY `join_address_fk` (`aid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `jointable` -- LOCK TABLES `jointable` WRITE; /*!40000 ALTER TABLE `jointable` DISABLE KEYS */; INSERT INTO `jointable` VALUES (1,1),(1,2); /*!40000 ALTER TABLE `jointable` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `person` -- DROP TABLE IF EXISTS `person`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `age` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `person` -- LOCK TABLES `person` WRITE; /*!40000 ALTER TABLE `person` DISABLE KEYS */; INSERT INTO `person` VALUES (1,'zhaoyunpeng',10); /*!40000 ALTER TABLE `person` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2012-08-17 0:17:14
//Address.java
- package com.zyp.examples;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.GenericGenerator;
- /**
- * Address entity. @author MyEclipse Persistence Tools
- */
- @Entity
- @Table(name=”address”)
- public class Address implements java.io.Serializable {
- private static final long serialVersionUID = 5136081851186696459L;
- // Fields
- @Id
- @GenericGenerator(name=”incrementGenerator”,strategy=”increment”)
- @GeneratedValue(generator=”incrementGenerator”, strategy=GenerationType.IDENTITY)
- @Column(name=”addressId”)
- private Integer addressId;
- @Column(name=”item”)
- private String item;
- // Constructors
- /** default constructor */
- public Address() {
- }
- /** full constructor */
- public Address(String item) {
- this.item = item;
- }
- // Property accessors
- public Integer getAddressId() {
- return this.addressId;
- }
- public void setAddressId(Integer addressId) {
- this.addressId = addressId;
- }
- public String getItem() {
- return this.item;
- }
- public void setItem(String item) {
- this.item = item;
- }
- }
package com.zyp.examples;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* Address entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="address")
public class Address implements java.io.Serializable {
private static final long serialVersionUID = 5136081851186696459L;
// Fields
@Id
@GenericGenerator(name="incrementGenerator",strategy="increment")
@GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY)
@Column(name="addressId")
private Integer addressId;
@Column(name="item")
private String item;
// Constructors
/** default constructor */
public Address() {
}
/** full constructor */
public Address(String item) {
this.item = item;
}
// Property accessors
public Integer getAddressId() {
return this.addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getItem() {
return this.item;
}
public void setItem(String item) {
this.item = item;
}
}
//Person.java
- package com.zyp.examples;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import org.hibernate.annotations.Fetch;
- import org.hibernate.annotations.FetchMode;
- import org.hibernate.annotations.GenericGenerator;
- /**
- * Person entity. @author MyEclipse Persistence Tools
- */
- @Entity
- @Table(name=”person”)
- public class Person implements java.io.Serializable {
- // Fields
- private static final long serialVersionUID = -5910642968209679728L;
- @Id
- @GenericGenerator(name=”incrementGenerator”,strategy=”increment”)
- @GeneratedValue(generator=”incrementGenerator”, strategy=GenerationType.IDENTITY)
- @Column(name=”id”)
- private Integer id;
- @Column(name=”name”, nullable=false)
- private String name;
- @Column(name=”age”, nullable=false)
- private Integer age;
- @OneToMany(targetEntity=Address.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
- @Fetch(FetchMode.SELECT)
- @JoinTable(joinColumns=@JoinColumn(name=”pid”, nullable=true),
- name=”jointable”, inverseJoinColumns=@JoinColumn(name=”aid”))
- private Set<Address> address = new HashSet<Address>();
- // Constructors
- /** default constructor */
- public Person() {
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return this.age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Set<Address> getAddress() {
- return address;
- }
- public void setAddress(Set<Address> address) {
- this.address = address;
- }
- }
package com.zyp.examples;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
/**
* Person entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="person")
public class Person implements java.io.Serializable {
// Fields
private static final long serialVersionUID = -5910642968209679728L;
@Id
@GenericGenerator(name="incrementGenerator",strategy="increment")
@GeneratedValue(generator="incrementGenerator", strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="name", nullable=false)
private String name;
@Column(name="age", nullable=false)
private Integer age;
@OneToMany(targetEntity=Address.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@JoinTable(joinColumns=@JoinColumn(name="pid", nullable=true),
name="jointable", inverseJoinColumns=@JoinColumn(name="aid"))
private Set<Address> address = new HashSet<Address>();
// Constructors
/** default constructor */
public Person() {
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
}
//HibernateTest.java
- package com.zyp.examples;
- import java.util.HashSet;
- import java.util.Set;
- import org.hibernate.Session;
- public class HibernateTest {
- public static void main(String[] args) {
- //级联添加
- // addByPerson();
- //级联删除
- deleteByPerson();
- }
- public static void deleteByPerson()
- {
- Session session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.delete(session.load(Person.class, new Integer(1)));
- session.getTransaction().commit();
- }
- public static void addByPerson()
- {
- Address a1 = new Address();
- a1.setItem(“item1″);
- Address a2 = new Address();
- a2.setItem(“item2″);
- Set<Address> address = new HashSet<Address>();
- address.add(a1);
- address.add(a2);
- Person p = new Person();
- p.setAge(10);
- p.setName(“zhaoyunpeng”);
- p.setAddress(address);
- Session session = HibernateUtil.getSessionFactory().openSession();
- session.beginTransaction();
- session.save(p);
- session.getTransaction().commit();
- }
- }
package com.zyp.examples;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
public class HibernateTest {
public static void main(String[] args) {
//级联添加
// addByPerson();
//级联删除
deleteByPerson();
}
public static void deleteByPerson()
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.delete(session.load(Person.class, new Integer(1)));
session.getTransaction().commit();
}
public static void addByPerson()
{
Address a1 = new Address();
a1.setItem("item1");
Address a2 = new Address();
a2.setItem("item2");
Set<Address> address = new HashSet<Address>();
address.add(a1);
address.add(a2);
Person p = new Person();
p.setAge(10);
p.setName("zhaoyunpeng");
p.setAddress(address);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
}
}
基于连接表的关联查询,最主要的就是 @jointable 注解的参数配置,看名字就知道。
既然是基于jointable的,那么name肯定是连接表的名字,joincolumn肯定是一端的关联列,inverseJoinColumns肯定是多端在连接表中的关联列,就这么简单…
2012年8月17日0:25:36
- hibernate_annotation_1ton_jointable_single.rar (5.7 MB)
- 下载次数: 7
