[英]hibernate how to extend one entity to all the entities
我有兩個字段應該出現在每個表中。 所以我想創建一個實體來保存這些字段,而我的其余實體繼承了這些字段但是當我運行查詢時,我收到錯誤 - 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 公共類 BaseTable ...
我建議更改命名約定 Base(Table) -> Base(Entity)。 對所有實體類執行相同操作。
您應該注意繼承策略 - https://en.wikibooks.org/wiki/Java_Persistence/Inheritance
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.