package com.baobaotao.domain;
…
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
![]()
@Entity(name = "T_TOPIC") ①
public class Topic implements Serializable ...{
@Id ②-1
@GeneratedValue(strategy = GenerationType.TABLE) ②-2
@Column(name = "TOPIC_ID") ②-3
private int topicId;
![]()
@Column(name = "TOPIC_TITLE", length = 100) ③
private String topicTitle;
![]()
@Column(name = "TOPIC_TIME")
@Temporal(TemporalType.DATE) ④
private Date topicTime;
![]()
@Column(name = "TOPIC_VIEWS")
private int topicViews;
![]()
//省略get/setter方法
}
…
@Entity(name = "T_TOPIC")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) ①
@DiscriminatorColumn(name = "TOPIC_TYPE", discriminatorType =
DiscriminatorType.INTEGER, length = 1) ②@DiscriminatorValue(value="1")③
public class Topic implements Serializable ...{
…
}
package com.baobaotao.domain;
…
@Entity
@DiscriminatorValue(value="2") ①
public class PollTopic extends Topic ...{②继承于Topic实体
private boolean multiple; ③
@Column(name = "MAX_CHOICES")
private int maxChoices;
@OneToMany(mappedBy="pollTopic",cascade=CascadeType.ALL) ④
private Set<PollOption> options = new HashSet<PollOption>();
//省略get/setter方法
}
package com.baobaotao.domain;
…
@Entity(name="T_POLL_OPTION")
public class PollOption implements Serializable ...{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "OPTION_ID")
private int optionId;
![]()
@Column(name = "OPTION_ITEM")
private String optionItem;
![]()
@ManyToOne ①
@JoinColumn(name="TOPIC_ID", nullable=false) ②
private PollTopic pollTopic;
}
第1页: JPA概述 | 第2页: 使用注解元数据 |
第3页: 使用XML元数据 | 第4页: JPA的编程结构及重要的API |
第5页: JPA的查询语言 |