欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate annotation用@SecondaryTable来实现从一个类对应多张表

程序员文章站 2022-04-14 09:15:25
...

上一篇文章中 Hibernate [email protected]共享主键关联,是以共享主键来实现的,

现在我们来 用@SecondaryTable来实现从一个类对应多张表

@Entity
@Table(name = "article")
@SecondaryTable(name="article_data",pkJoinColumns={@PrimaryKeyJoinColumn(name="article_id")})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Aricle extends IdEntity<Integer> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8056490229900614401L;
	private String title;
	private String subTitle;
	private Date addTime;
	private String content;
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubTitle() {
		return subTitle;
	}

	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
@Column(name="content",table="article_data")
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

 重点在这:

@SecondaryTable(name="article_data",pkJoinColumns={@PrimaryKeyJoinColumn(name="article_id")})   

 

@Column(name="content",table="article_data")
但是 发现不能延迟加载了,我分两张表的目的就是为了延迟加载, OHNO

参考:

用@SecondaryTable实现多表联合到一个实体

@SecondaryTable 进行多表映射

 常用 JPA annotation 参考