开发者

Hibernate: Unable to resolve attribute against path for a very simple object hierarchy

Anyone have any ideas why hibernate v3.6.7 has problems with the seemingly simple class hierarchy below?

@Entity
public class X implements Serializable {
    @Id
    String id;

    X() {

    }

    public X(String id) {
        this.id = id;
    }
}

interface PK {
    Object getPK();
}

@Entity
public class Y  implements Serializable, PK {
    @Id
    @OneToOne
    @JoinColumn(name ="id")
    X x;

    protected Y() {

    }

    public Y(X x) {
        this.x = x;
    }

    @Override
    public Object getPK() {
        return x.id;
    }
}

private static final EntityManager em;
private final CriteriaBuilder cb = em.getCriteriaBuilder();
private final CriteriaQuery<Y> query = cb.createQuery(Y.class);
private final Root<Y> entity = query.from(Y.class);

static {
    Map<String, Object> properties = new HashMap<String, Object>();
        // initialise properties appropriately
    EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("myPersistenceUnit", properties);
    em = emf.createEntityManager();

}

@Test
public void simpleTest() {
    X x1 = new X("X1");
    X x2 = new X("X1");
    List<Y> yy = new ArrayList<Y>();

    Y yX1 = new Y(x1);
    yy.add(yX1);

    Y yX2 = new Y(x2);
    yy.add(yX2);

    saveItems(yy);

    String name = "x";
    Path<Object> path = entity.get(name);
    Predicate restriction = cb.conjunction();
    restriction = cb.and(restriction, cb.and(new Predicate[]{cb.equal(path, x1)}));

    TypedQuery<Y> tq = em.createQuery(this.query);
    Y result = null;

    try {
        result = tq.getSingleResult();
    } catch (NoResultException e) {
    }

    assertNotNull(result);
}

attempting to execute this test throws the following excepti开发者_JS百科on:

java.lang.IllegalArgumentException: Unable to resolve attribute [x] against path
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:118)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:223)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:194)
    at com.example.entity.impl.MyPersistenceTest.simpleTest(MyPersistenceTest.java:212)


I have the feeling that the mapping is a bit wrong.

First, X is missing @Embeddable and Y.x should be mapped as @EmbeddedId, not as @Id.

Extending that Bozho said, if you want to use an class as both ID and Entity, you need to create a super class and one subclass of each usage (PK and entity) with the mapping annotations.


Whats this ?

X() {

}

i think there should be an empty constructor!

public X(String id) {
    this.id = id;
}

why do you use the @id annotation on a class --> X i think a generated value like an id must be an long!

cu


Note sure what the problem is, but things to look at:

  • you shouldn't use another entity and an @Id. I'm not sure if this is possible. Use Embeddable or @IdClass
  • provide getter and setter for fields.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜