2016-09-29 159 views
0

我有兩個字段應該出現在每個表中。所以我想創建一個實體,將持有這些領域,其餘的我的實體繼承這些領域 但是,當我運行查詢時,我得到的錯誤 - 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(); 
    } 
} 
+2

'BaseTable'必須是一個實體,以及或映射超一流的。嘗試將'@ MappedSuperclass'添加到'BaseTable'中。 – Thomas

+0

您不需要在'@ Entity'中指定表名。帶名稱的「@ Table」就足夠了。 –

+0

你有沒有任何文件詳細說明如何命名約定 –

回答