Tung Vo
Tung Vo

Reputation: 2549

OneToMany, org.hibernate.QueryException: could not resolve property

I my project I have to class

@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..**/
}

and another class

@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..**/
}

Data Access Object like this

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

when i run the code. I get the error

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: lastPositionId of: com.abc.server.db.entity.Asset [FROM com.abc.server.db.entity.Position pos WHERE pos.deviceId IN ('100 000000001000','100000000001001','100000000001002') AND pos.id IN (SELECT asset.lastPositionId FROM com.tma.ats.am.server .db.entity.Asset asset)]

I found the problem is Hibernate generate query with field name lastPositionId Instead of lastPosition. I change field name from lastPosition to lastPositionId. Everything work Ok. But I read many example on web and from my own project. All other field can map @ManyToOne Ok and they don't need Postfix Id. How can i make above code work with field name lastPosition (not lastPositionId) ? Thank for any help.

Upvotes: 0

Views: 935

Answers (1)

over9k
over9k

Reputation: 115

there is a property in your jpa xml configuration file called hibernate.hbm2ddl.auto. Change its value to create or create-drop to apply new changes.

Upvotes: 0

Related Questions