hibernate查询表的部分属性
程序员文章站
2022-04-14 09:15:01
...
由于一个表中含有比较占内存的字段,当查询的结果比较大时,可能会造成内存泄漏,所以想在查询的时候不加载那个字段,具体类如下:
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.search.annotations.DocumentId;
/**
* 网上记账实体类
* @since 2011-3-4
* @author
* @comment
*/
@Entity
@Table(name = "TOOLACCOUNT")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ToolAccount implements Serializable {
private static final long serialVersionUID = 1L;
/** 主键 * */
@Id
@Column(name = "TOOLACCOUNTPK", length = 19)
@GeneratedValue(generator = "custom-id")
@GenericGenerator(name = "custom-id", strategy = "lab24.util.TableKeyGenerator", parameters = {
@Parameter(name = "table", value = "KEYGENERATOR_TABLE"),
@Parameter(name = "pkColumnName", value = "G_KEY"),
@Parameter(name = "pkColumnValue", value = "TOOLACCOUNT_PK"),
@Parameter(name = "valueColumnName", value = "G_VALUE"),
@Parameter(name = "allocationSize", value = "1") })
@DocumentId
private long toolAccountPK;
private String title;//名称
private Date createDate;//创建日期
private double money ;//金额
private String content;//内容
private int inOrOut;//收入还是支出 0:收入 1:支出
private String inOrOutType;//收入与支出的明线条目
/** 关联用户 * */
@ManyToOne(optional = false)
@JoinColumn(name = "USERPK", nullable = false)
private User user;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getInOrOut() {
return inOrOut;
}
public void setInOrOut(int inOrOut) {
this.inOrOut = inOrOut;
}
public String getInOrOutType() {
return inOrOutType;
}
public void setInOrOutType(String inOrOutType) {
this.inOrOutType = inOrOutType;
}
public long getToolAccountPK() {
return toolAccountPK;
}
public void setToolAccountPK(long toolAccountPK) {
this.toolAccountPK = toolAccountPK;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
这里的content是比较大的信息的字段,而作统计账单信息时,它是用不上的,所以想查询的时候不要这个字段,这在hibernate 也是可以做到的,这里是使用一个新的类来存放查询的结果,因为hibernate是以封装好的对象返回来的,所以这里要新建一个对象,这个和 下一篇: hibernate 一对多缓存的问题