我的项目我必须上课
@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:com.abc.server.db.entity.Asset [FROM com.abc.server.db.entity.Position pos WHERE pos.deviceId IN (&#39; 100 000000001000&#39;,&#39; 100000000001001&#39;,&#39; 100000000001002&#39;)和pos.id IN (SELECT asset.lastPositionId FROM com.tma.ats.am.server .db.entity.Asset asset)]
我发现问题是Hibernate使用字段名lastPositionId生成查询而不是lastPosition。我将字段名称从lastPosition更改为lastPositionId。一切正常。但我在网上和我自己的项目中阅读了很多例子。所有其他字段都可以映射@ManyToOne Ok,他们不需要Postfix Id。如何使上面的代码使用字段名称lastPosition(而不是lastPositionId)? 感谢您的帮助。
答案 0 :(得分:0)
你的jpa xml配置文件中有一个名为hibernate.hbm2ddl.auto的属性。将其值更改为create或create-drop以应用新更改。