java - hibernate.QueryException : could not resolve property

标签 java hibernate

这是此错误的另一种情况:

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]

我有以下设置:

查询.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 注释可能有问题。只是我加入的列名来自为列名别名的 View 。 但也许还有其他问题。我对此还很陌生。

DeviceType 的片段:

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;
}

感谢您的帮助!

最佳答案

我发现我的数据库映射根本不正确。我有一个 n:m 关系,这对于 hibernate 来说似乎并不容易。但这非常有帮助: Hibernate Many-To-Many Revisited

但这仍然没有解决问题。我发现我有复合主键,两个表的主键映射在 n:m 表中。另一个不那么容易的设置。所以我关注了这个线程:Mapping ManyToMany with composite Primary key and Annotation:

第二个链接中的配置以及根据第一个链接中的第二个策略的 SQL 语句是有效的。

关于java - hibernate.QueryException : could not resolve property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14761181/

相关文章:

java - 客户端身份验证 (SSL) 直通?

hibernate - 从Grails应用程序执行的MySQL存储过程调用执行起来非常缓慢

java - 保存一对一关系: Null pointer Exception

java - 使用 Tapestry5 删除 hibernate 实体给出 : "a different object with the same identifier value was already associated with the session"

java - 如何用Java封装事务模式

java - 我不知道如何从 EditText 获取输入以传递到另一个文件

java - 帮助编写 REGEX

java - Hibernate 中同一个表中的一对多映射

java - 使用反射在 Java 中动态显式转换原始类型

java - 使用 hibernate 注释预先加载持久对象