EJB3.0里的@SecondaryTable记录

阅读更多

EJB3.0里的@SecondaryTable 可以将一个 entity bean 对应数据库里的多张表,

下面的代码是仿照javaee doc 上写的,测试通过。

 

数据表图:

 

EJB3.0里的@SecondaryTable记录_第1张图片

 

 

 

 

 

 

 

 

 

 

 

@SuppressWarnings("serial")
@Entity
@Table(name="part")
@SecondaryTable(
		name="partdetail",
		pkJoinColumns={
			@PrimaryKeyJoinColumn(name="partnumber",referencedColumnName="partnumber"),
			@PrimaryKeyJoinColumn(name="revision",referencedColumnName="revision")
		}
)
@IdClass(PartKey.class)
public class Part implements Serializable {
	private String partNumber;
	private Integer revision;
	private String partDescription;
	private Date revisionDate;
	
	private Part bomPart;
	private List parts;
	
	private Serializable drawing;
	private String specification;
			
	@Column(table="partdetail")
	@Lob
	public Serializable getDrawing() {
		return drawing;
	}
	public void setDrawing(Serializable drawing) {
		this.drawing = drawing;
	}
	
	@Column(table="partdetail")
	@Lob
	public String getSpecification() {
		return specification;
	}
	public void setSpecification(String specification) {
		this.specification = specification;
	}
	
	@Id
	@Column(name="partnumber",nullable=false)
	public String getPartNumber() {
		return partNumber;
	}
	public void setPartNumber(String partNumber) {
		this.partNumber = partNumber;
	}
	
	@Id
	@Column(name="revision",nullable=false)
	public Integer getRevision() {
		return revision;
	}
	public void setRevision(Integer revision) {
		this.revision = revision;
	}
	
	@Column(name="partdescription")
	public String getPartDescription() {
		return partDescription;
	}
	public void setPartDescription(String partDescription) {
		this.partDescription = partDescription;
	}
	
	@Temporal(TemporalType.DATE)
	public Date getRevisionDate() {
		return revisionDate;
	}
	public void setRevisionDate(Date revisionDate) {
		this.revisionDate = revisionDate;
	}
	
	@ManyToOne
	@JoinColumns({
		@JoinColumn(name="bompartnumber",referencedColumnName="partnumber"),
		@JoinColumn(name="bomrevision",referencedColumnName="revision")
	})
	public Part getBomPart() {
		return bomPart;
	}
	public void setBomPart(Part bomPart) {
		this.bomPart = bomPart;
	}
	
	@OneToMany(mappedBy="bomPart",cascade=CascadeType.ALL)
	public List getParts() {
		return parts;
	}
	public void setParts(List parts) {
		this.parts = parts;
	}
	
	
}

 

其中:

 

@SecondaryTable(
		name="partdetail",//表示从表名
		pkJoinColumns={// 从表到主表的主键映射
			@PrimaryKeyJoinColumn(name="partnumber",referencedColumnName="partnumber"),
			@PrimaryKeyJoinColumn(name="revision",referencedColumnName="revision")
			// 其中:name为从表字段,referencedColumnName为主表字段
		}
)
 

还有映射字段时注意加上表名:

 

	@Column(table="partdetail")
	@Lob
	public Serializable getDrawing() {
		return drawing;
	}
	
	@Column(table="partdetail")
	@Lob
	public String getSpecification() {
		return specification;
	}
 

你可能感兴趣的:(JavaEE,Bean)