【问题标题】:OneToMany, org.hibernate.QueryException: could not resolve propertyOneToMany,org.hibernate.QueryException:无法解析属性
【发布时间】:2015-03-02 10:43:33
【问题描述】:

我的项目我必须上课

@Entity
@Table(name = "asset")
public class Asset extends BaseEntity {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "last_position")
private Position lastPosition;

/**contructor, getter, setter method, other field etc..**/
}

和另一个班级

@Entity
@Table(name = "position")
@EntityListeners(EntityChangeCallback.class)
public class Position extends BaseEntity {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy="lastPosition")
private List<Asset> assets;

/**contructor, getter, setter method, other field etc..**/
}

像这样的数据访问对象

public abstract class BaseDaoImp<E extends Indexable> implements BaseDao<E> {
@PersistenceContext
protected EntityManager em;
/**some thing else**/
@SuppressWarnings("unchecked")
public List<E> list() {
    Query qry = em.createQuery("from " + getType().getSimpleName() + " u");
    return qry.getResultList();
}
}

当我运行代码时。我得到了错误

引起:java.lang.IllegalArgumentException: org.hibernate.QueryException:无法解析属性: lastPositionId of: com.abc.server.db.entity.Asset [FROM com.abc.server.db.entity.Position pos WHERE pos.deviceId IN ('100 000000001000','100000000001001','100000000001002') 和 pos.id IN (从 com.tma.ats.am.server 中选择asset.lastPositionId .db.entity.Asset 资产)]

我发现问题是 Hibernate 使用字段名称 lastPositionId 而不是 lastPosition 生成查询。我将字段名称从 lastPosition 更改为 lastPositionId。一切正常。但我在网上和我自己的项目中阅读了很多例子。所有其他字段都可以映射@ManyToOne Ok,它们不需要 Postfix Id。我怎样才能使上面的代码与字段名称 lastPosition (不是 lastPositionId) 一起工作? 感谢您的帮助。

【问题讨论】:

    标签: java spring hibernate jpa ejb


    【解决方案1】:

    在您的 jpa xml 配置文件中有一个名为 hibernate.hbm2ddl.auto 的属性。将其值更改为 create 或 create-drop 以应用新更改。

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      相关资源
      最近更新 更多