2013-02-07 124 views
1

下面是此錯誤的另一種情況:hibernate.QueryException:無法解析屬性

21:22:15,881 ERROR [SessionFactoryImpl] Error in named query: ch.software.gvs.TroubleNotification_DeviceType.byType org.hibernate.QueryException: 
could not resolve property: type of: ch.ildsoftware.gvs.TroubleNotification_DeviceType 
[select d.id from ch.ildsoftware.gvs.TroubleNotification_DeviceType d where d.type = :type] 

我有以下設置:

queries.xml:

<named-query name="ch.ildsoftware.gvs.TroubleNotification_DeviceType.byType"> 
    <query> 
    select t.id from TroubleNotification_DeviceType t where t.type = :type 
    </query>   
</named-query> 

TroubleNotification_DeviceType.java

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 

@Entity 
@Table(name = "tblgwTroubleNotification_ADSTyp") 
public class TroubleNotification_DeviceType implements Serializable { 

private static final long serialVersionUID = 1L; 

private TroubleNotification id; 
private DeviceType type; 
private String createdBy; 
private String createdDate; 

public TroubleNotification_DeviceType() 
{} 

public TroubleNotification_DeviceType(TroubleNotification id, DeviceType type) { 
    this.id = id; 
    this.type = type; 
} 

@Id 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "IDgwTroubleNotification", nullable = false) 
public TroubleNotification getId() { 
    return id; 
} 

public void setId(TroubleNotification id) { 
    this.id = id; 
} 

@Id 
@Column(name = "ADSTypID", nullable = false) 
@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "GeraeteTypID", nullable = false) 
public DeviceType getType() { 
    return type; 
} 

public void setType(DeviceType type) { 
    this.type = type; 
} 

@Column(name = "Created", nullable = false) 
public String getCreatedBy() { 
    return createdBy; 
} 

public void setCreatedBy(String createdBy) { 
    this.createdBy = createdBy; 
} 

@Column(name = "CreatedDate", nullable = false) 
public String getCreatedDate() { 
    return createdDate; 
} 

public void setCreatedDate(String createdDate) { 
    this.createdDate = createdDate; 
} 
} 

我懷疑@Column和@JoinColumn註釋可能有問題。只是我加入的列名來自一個視圖,它將列名稱進行了別名。 但也許別的東西是錯的。我對此很新穎。

片斷出設備類型的:

private static final long serialVersionUID = 1L; 
private Integer id; 
private String name; 

    .... 

@Id 
@Column(name = "GeraeteTypID", nullable = false) 
public Integer getId() 
{ 
    return this.id; 
} 

在其它類的引用將是這樣的,並且很好地工作(然而,列名是相同的):

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "GeraeteTypID", nullable = false) 
public DeviceType getType() 
{ 
    return this.type; 
} 

片斷出EJB的:

@Override 
@SuppressWarnings("unchecked") 
public List<TroubleNotification> getTroubleNotificationByDeviceType(DeviceType aType) 
{ 
    // first get all IDgwTroubleNotification for ADSTypID 
    Query idSet = gvsData.createNamedQuery(
      TroubleNotification_DeviceType.class.getName() + ".byType"); 
    idSet.setParameter("type", aType); 
    List<TroubleNotification> idSetResult = idSet.getResultList(); 

    final List<TroubleNotification> troubleNotificationResult = new ArrayList<TroubleNotification>(); 
    for (int i = 0; i < idSetResult.size(); i++) { 
     // get all Notification for IDgwTroubleNotification 
     Query notificationById = gvsData.createNamedQuery(
       TroubleNotification.class.getName() + ".byId"); 
     notificationById.setParameter("id", idSetResult.get(i)); 
     troubleNotificationResult.add((TroubleNotification) notificationById.getResultList()); 
    } 
    return troubleNotificationResult; 
} 

謝謝你的幫忙!

+0

嘗試在DeviceType上添加@Embaddable – twillouer

+0

它表示:'AnnotationException:ch.software.gvs.DeviceType在用作@ EmbeddedId時不能有@Id屬性,並且在DeviceType中沒有@ID時,錯誤'AnnotationException:No標識符指定爲實體:ch.ildsoftware.gvs.DeviceType'來。 Changind @Id to @EmbeddedId帶來'AnnotationException:java.lang.Integer沒有持久性id屬性' –

+0

在此期間,我改變了很多代碼。 DB中有以下情況:'故障通知< - TroubleNotificationDeviceType - > DeviceType'。我遵循了[鏈接] [http://planet.jboss.org/post/hibernate_many_to_many_revisited]的建議。查詢是不同的,但似乎很好:'從DeviceType選擇tn dt加入dt.troubleNotifications tn其中dt.id =:id'。這個星座中的新錯誤是'MappingException:無法確定類型:java.util.Set,在table:tblgwTroubleNotification,列:[org.hibernate.mapping.Column(IDgwTroubleNotification)]'。 –

回答

0

我發現我的數據庫映射並不合適。我有一個n:m的關係,它看起來並不像hibernate那樣容易。但這是非常有幫助的: Hibernate Many-To-Many Revisited

但是,這仍然沒有解決問題。我發現我有複合主鍵,兩個表的主鍵映射在n:m表中。另一個不太容易的設置。所以我遵循這個線程:Mapping ManyToMany with composite Primary key and Annotation:

從第二個鏈接的配置,連同第一個鏈接中的第二個策略的SQL語句一起工作。

相關問題