我有两个字段应该出现在每个表中。所以我想创建一个实体来保存这些字段,我的其他实体继承了这些字段
但是,当我运行查询时,出现错误 - org.hibernate.QueryException: could not resolve property: active of: com.db.tables.PersonTable
我做错了什么?
所有实体的基类都应该继承这些字段
@XmlRootElement
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class BaseTable implements Serializable
{
private static final long serialVersionUID = 1L;
@Column(name = "Updated")
@JsonProperty
@NotNull
protected Timestamp updated;
@Column(name = "Active")
@JsonProperty
@NotNull
protected byte active;
public BaseTable ()
{
active = (byte)1;
updated = DbUtils.getCurrentTimeStamp();
}
public byte getActive()
{
return active;
}
public void setActive(byte active)
{
this.active = active;
}
public Timestamp getUpdated()
{
return updated;
}
public void setUpdated(Timestamp updated)
{
this.updated = updated;
}
@Override
public String toString()
{
return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + active;
result = prime * result + ((updated == null) ? 0 : updated.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
BaseTable other = (BaseTable) obj;
if (active != other.active) return false;
if (updated == null)
{
if (other.updated != null) return false;
}
else if (!updated.equals(other.updated)) return false;
return true;
}
}
继承的类
@Entity(name = "Persons")
@Table(name = "Persons")
public class PersonTable extends BaseTable implements Serializable
{
private static final long serialVersionUID = -5793514680136648542L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PersonId")
private short personId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="personId")
PortalUserTable portalUser;
//getters&settersand more fields
}
多一个类继承
@Entity(name = "PortalUser")
@Table(name = "PortalUser")
public class PortalUserTable extends BaseTable implements Serializable
{
private static final long serialVersionUID = -5793514680136648542L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PersonId")
private short personId;
@OneToOne
(mappedBy = "portalUser")
PersonTable person;
//getters&settersand more fields
}
查询
public ObjectDaoResponse getAllTigrisUsers() throws JsonProcessingException
{
try
{
Query q = sessionFactory.getCurrentSession().createQuery("SELECT new com.db.queries.users.User( u.portalUserId ,p.personName) FROM PortalUsers u INNER JOIN u.person p WHERE portalUserId = p.personId AND p.active = 1 AND u.active = 1");
List<TigrisUser> l = q.list();
return ObjectDaoResponse.getAnOkResponse(l);
}
catch(Exception e)
{
System.out.println(e);
return ObjectDaoResponse.getGeneralFailureResponse();
}
}
最佳答案
@MappedSuperclass 公共(public)类 BaseTable ...
我建议更改命名约定 Base(Table) -> Base(Entity)。对所有实体类执行相同的操作。
您应该注意继承策略 - https://en.wikibooks.org/wiki/Java_Persistence/Inheritance
关于java - hibernate 如何将一个实体扩展到所有实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769645/