近三天jpa学习

创建完persistence.xml后,需要对数据库中表进行entity映射,当然,也可以不事先创建表,使用在persistence中的配置来自动创建数据库中的表。我创建了两个实体entity,这两个实体entity我已经在数据库中事先创建好表和他们对应。

testuser类


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

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.tao3c.dao.impl.TestUserDaoImpl;


@Entity
@Table(name = "test_user")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "com.XXX.entity.TestUserEntity")
@NamedQueries({
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETALLTESTUSER,query=TestUserDaoImpl.TESTUSER_GETALLUSER),
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETTESTUSERBYCONDITION,query=TestUserDaoImpl.TESTUSER_GETUSERBYCONDITION),
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETTESTUSERNORELATIONJOIN,query=TestUserDaoImpl.TESTUSER_GETNORELATIONJOIN)
})
public class TestUserEntity extends AbstractBaseEntity {

	private static final long serialVersionUID = -7139178611703146047L;
    public TestUserEntity(){
    	this.emails = new HashSet<TestEmailEntity>();
    }
	/**
	 * int TestUserEntity.java $id值
	 */
	@Id
	@TableGenerator(name = "TestUserGenerator", table = "id_testuser", pkColumnName = "GEN_KEY", pkColumnValue = "com.tao3c.testuser", valueColumnName = "GEN_VALUE", allocationSize = 10, initialValue = 11)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "TestUserGenerator")
	@Column(name = "id", unique = true, nullable = false)
	private int id;
	@Column(name = "name")
	private String name;
	@Temporal(TemporalType.DATE)
	private Date regtime;
	@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.REMOVE,CascadeType.MERGE},mappedBy="testuser")
	private Set<TestEmailEntity> emails;
	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;
	}

	public Date getRegtime() {
		return regtime;
	}

	public void setRegtime(Date regtime) {
		this.regtime = regtime;
	}

	public Set<TestEmailEntity> getEmails() {
		return emails;
	}

	public void setEmails(Set<TestEmailEntity> emails) {
		this.emails = emails;
	}
	public void addTestEmail(TestEmailEntity entity){
		this.emails.add(entity);
	}
}
testemails类

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="test_emails")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "com.XXX.entity.TestUserEntity")
public class TestEmailEntity {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
    private int id;
	@Column(name="userid")
    private int userid;
	@Column(name="emailname")
    private String emailname;
	@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH,CascadeType.PERSIST,CascadeType.REMOVE},optional=false)
	@JoinColumn(name="user_id")
	private TestUserEntity testuser;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public String getEmailname() {
		return emailname;
	}
	public void setEmailname(String emailname) {
		this.emailname = emailname;
	}
	public TestUserEntity getTestuser() {
		return testuser;
	}
	public void setTestuser(TestUserEntity testuser) {
		this.testuser = testuser;
	}
}

 首先说明下,在testuser类中可以存在多个testemail 用来测试jpa的一对多和多对已的关系。

在testuser中

@Entity
@Table(name = "test_user")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "com.XXX.entity.TestUserEntity")
@NamedQueries({
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETALLTESTUSER,query=TestUserDaoImpl.TESTUSER_GETALLUSER),
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETTESTUSERBYCONDITION,query=TestUserDaoImpl.TESTUSER_GETUSERBYCONDITION),
	@NamedQuery(name=TestUserDaoImpl.QUERY_GETTESTUSERNORELATIONJOIN,query=TestUserDaoImpl.TESTUSER_GETNORELATIONJOIN)
})

entity声明这是一个实体类,table声明了数据库中表的名称,如果不使用缓存策略的话可以将cacheable和cache去除,在cache中的region声明了缓存策略,这是在配置文件中配置的。namedqueries中事先定义了所要使用的sql,在查询中是使用类似于这样的语句进行查询

Query query  = entityManager.createNamedQuery(QUERY_GETTESTUSERBYCONDITION);

 同样也可以使用原生的jpql进行查询,直接使用jpql进行查询使代码看起来不简洁,对以后的维护不利。

/**
	 * int TestUserEntity.java $id值
	 */
	@Id
	@TableGenerator(name = "TestUserGenerator", table = "id_testuser", pkColumnName = "GEN_KEY", pkColumnValue = "com.tao3c.testuser", valueColumnName = "GEN_VALUE", allocationSize = 10, initialValue = 11)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "TestUserGenerator")
	@Column(name = "id", unique = true, nullable = false)
	private int id;

 在这里id声明了entity的主键,TableGenerator对应于GeneratedValue中的generator,因为使用的主键生成策略是GenerationType.TABlE 既它的id键值是通过数据库中的一个表来生成的,这个你表的名字为id_testuser 该表有两个字段分别为gen_key 和gen_value 其实这个表中只需要一列就可以了, gen_key的值变为com.tao3c.testuser,gen_value的起始值为11,如果在这个表中没有11这一行,那么hibernate的实现中对initValue值是不起作用的这就需要自己手动的将该行插入到表中,设置gen_value的值为11,allocationSize 为每次取出多少的id值供后续使用,该属性不能取的太大,如果你频繁重启服务器的话,就会使id的值以allocationSize间隔跳跃。

@Temporal(TemporalType.DATE)
	private Date regtime;

为时间字段的映射

.......后续的内容在写了,先干活l

相关推荐