`

hibernate @any

阅读更多
copy from
http://my.oschina.net/longyuan/blog/337635?p={{page}}#OSC_h3_3
http://www.iteye.com/topic/8900


package com.bjsxt.hibernate.any;

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

@Entity
public class Person {

	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;
	}

}

package com.bjsxt.hibernate.any;

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

@Entity
public class Company {

	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;
	}

}


package com.bjsxt.hibernate.any;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

import org.hibernate.annotations.Any;
import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.MetaValue;

@Entity
public class Vehicle {
	private int id;
	private String name;

	private Object ownObject;

	@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;
	}

	@Any(metaColumn = @Column(name = "owner_type", length = 3), fetch = FetchType.LAZY)
	@AnyMetaDef(idType = "int", metaType = "string", metaValues = {
			@MetaValue(targetEntity = com.bjsxt.hibernate.any.Person.class, value = "1"),
			@MetaValue(targetEntity = com.bjsxt.hibernate.any.Company.class, value = "2") })
	@JoinColumn(name = "owner_id")
	public Object getOwnObject() {
		return ownObject;
	}

	public void setOwnObject(Object ownObject) {
		this.ownObject = ownObject;
	}

}


package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.bjsxt.hibernate.any.Company;
import com.bjsxt.hibernate.any.Person;
import com.bjsxt.hibernate.any.Vehicle;

public class TestAny {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Configuration cfg = new AnnotationConfiguration();
		SessionFactory sf = cfg.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		Person p = new Person();
		p.setId(1);
		p.setName("1111111");

		Vehicle v = (Vehicle) session.load(Vehicle.class, 2);
		//Person pe = (Person) v.getOwnObject();
		//System.out.println("p:" + pe.getId() + "name:" + pe.getName());
		Company c = (Company) v.getOwnObject();
		System.out.println("c:" + c.getId() + "name:" + c.getName());
		session.getTransaction().commit();
		session.close();
		sf.close();
		System.out.println("------------------end---------------------");
	}

}



分享到:
评论

相关推荐

    Beginning Hibernate: For Hibernate 5

    Experienced authors Joseph Ottinger with Dave Minter and Jeff Linwood provide more in-depth examples than any other book for Hibernate beginners. They present their material in a lively, example-...

    hibernate-shards.jar

    any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any ...

    Hibernate+中文文档

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用...

    hibernate3.2中文文档(chm格式)

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用...

    hibernate 教程

    映射到"任意"(any)类型 5.3. SQL中引号包围的标识符 5.4. 映射文件的模块化(Modular mapping files) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent Collections) 6.2. 映射集合...

    HibernateAPI中文版.chm

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用...

    Beginning.Hibernate.For.Hibernate.5.4th.Edition

    Experienced authors Joseph Ottinger with Dave Minter and Jeff Linwood provide more in-depth examples than any other book for Hibernate beginners. They present their material in a lively, example-...

    Beginning Hibernate, 3rd Edition

    Experienced authors Joseph Ottinger with Dave Minter and Jeff Linwood provide more in-depth examples than any other book for Hibernate beginners. The authors also present material in a lively, ...

    Hibernate中文详细学习文档

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用...

    Spring.Persistence.with.Hibernate.2nd.Edition.1484202694

    Because the book covers several persistence frameworks, it is suitable for anyone interested in learning more about Spring or any of the frameworks covered. Lastly, this book covers advanced topics ...

    Hibernate 中文 html 帮助文档

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet...

    最全Hibernate 参考文档

    5.1.21. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. SQL中引号包围的标识符 5.4. 其他元数据(Metadata) 5.4.1. 使用 XDoclet 标记 5.4.2. 使用 JDK ...

    hibernate-entitymanager-3.2.1.GA

    THIS RELEASE OF HIBERNATE ENTITYMANAGER REQUIRES HIBERNATE CORE 3.2.0.GA (and above) AND DOES NOT WORK WITH HIBERNATE 3.1.x OR ANY OLDER VERSION OF HIBERNAT

    hibernate 体系结构与配置 参考文档(html)

    any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 ...

    Hibernate教程

    6.1.21. any 6.2. Hibernate 的类型 6.2.1. 实体(Entities)和值(values) 6.2.2. 基本值类型 6.2.3. 自定义值类型 6.3. SQL中引号包围的标识符 6.4. 其他元数据(Metadata) 6.4.1. 使用 XDoclet 标记 6.4.2. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用...

    hibernate动态映射表处理oracle的clob类型

    hibernate动态映射表处理oralce10g的clob类型,压缩包为整个工程包. 真诚地希望能帮到你,同时真诚地希望你能和我交流, about any question^_^

    hibernate

    映射到"任意"(any)类型 5.3. SQL中引号包围的标识符 5.4. 映射文件的模块化(Modular mapping files) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent Collections) 6.2. 映射集合...

    Hibernate Reference Documentation3.1

    5.1.22. any 5.2. Hibernate Types 5.2.1. Entities and values 5.2.2. Basic value types 5.2.3. Custom value types 5.3. Mapping a class more than once 5.4. SQL quoted identifiers 5.5. Metadata ...

    Hibernate3的帮助文档

    6.1.21. any 6.2. Hibernate 的类型 6.2.1. 实体(Entities)和值(values) 6.2.2. 基本值类型 6.2.3. 自定义值类型 6.3. SQL中引号包围的标识符 6.4. 其他元数据(Metadata) 6.4.1. 使用 XDoclet 标记 6.4.2. ...

Global site tag (gtag.js) - Google Analytics