数据库表
CREATE TABLE WIES.DEPARTMENT
(
DEPT_ID integer NOT NULL
GENERATED BY DEFAULT
AS IDENTITY (
START WITH 100,
INCREMENT BY 1),
DEPT_NME varchar(20) ,
SEQUENCE_NO integer ,
ACTIVE_IND char(1) DEFAULT 'Y'
);
实体类
@Entity
@Table(name = "DEPARTMENT", schema = "WIES")
public class Department implements JpaObjectWithName, Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "DEPT_ID", unique = true, nullable = false)
private Integer departmentId;
@Column(name = "SEQUENCE_NO")
private int sequence;
@Column(name = "DEPT_NME")
@Size(max = 20, message = "error.department.name.length")
private String name;
@Transient
private boolean active;
public Integer getDepartmentId()
{
return departmentId;
}
public void setDepartmentId(Integer departmentId)
{
this.departmentId = departmentId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getSequence()
{
return sequence;
}
public void setSequence(int sequence)
{
this.sequence = sequence;
}
public boolean isActive()
{
return active;
}
public void setActive(boolean active)
{
this.active = active;
}
@Column(name = "ACTIVE_IND")
public String getActiveString()
{
return BooleanUtils.toString(active, "Y", "N");
}
public void setActiveString(String activeString)
{
this.active = BooleanUtils.toBoolean(activeString);
}
@Override
public String toString()
{
return (this.name != null)
? this.name : super.toString();
}
}
DAO
@SuppressWarnings("unchecked")
public List<Department> getDepartments()
{
String jql = "select a from Department a";
Query query = entityManager.createQuery(jql
+ " order by a.sequence, a.name");
return (List<Department>) query.getResultList();
}
从控制台生成的sql语句
2014-06-09 16:31:34,282 DEBUG [org.hibernate.SQL] - <select department0_.DEPT_ID as DEPT_ID1_9_, department0_.DEPT_NME as DEPT_NME2_9_, department0_.SEQUENCE_NO as SEQUENCE3_9_ from WIES.DEPARTMENT department0_ order by department0_.SEQUENCE_NO, department0_.DEPT_NME>
select语句缺少列ACTIVE_IND
。如何在select语句中包含它而不在Department类中添加另一个要填充的字段?
原因是我想在boolean
字段中使用表单上的复选框,并将其存储到数据库中,它必须是'Y'
或'N'
以及在表格中显示'Y'
或'N'
。
答案 0 :(得分:0)
这有点非弹性,但在课前的注释部分,您可以添加WHERE注释,如下所示:
@Entity
@Table(name = "DEPARTMENT", schema = "WIES")
@Where(clause = "active_ind = 'Y'" )
public class Department implements JpaObjectWithName, Serializable
{
.....
}
这样,只会选择处于活动状态的记录。
答案 1 :(得分:0)
您必须在类上添加注释@Access(AccessType.FIELD),在getActiveString()方法上添加注释@Access(AccessType.PROPERTY)并将@Transient保留在活动字段上。