I have 2 entities, say Car and Engine (just example names, but the logic is the same).
@Entity
public class Car {
@Id
private Long id;
@OneToOne(mappedBy = "car", cascade = Cascade.ALL)
private Engine engine;
...
}
@Entity
public class Engine {
@Id
private Long id; // 1
@MapsId // 2
@OneToOne
@JoinColumn(name = "car_id") // join column is optional here
private Car car;
...
}
So, then I do:
em.save(car); // successfully saved, data is in the database, but (see below)
TypedQuery<Engine> query = em.createQuery("select engine from Engine engine where engine.car = :car", Engine.class)
query.setParameter("car", car);
query.getResultList();
throws exception:
ERROR [main] (JDBCExceptionReporter.java:234) - No value specified for parameter 1.
WARN [main] (TestContextManager.java:409) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@17e5cbd] to process 'after' execution for test: method , exception [org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.DataException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query]
However, if I change Engine entity to have only @Id on the car instance itself (removing //1 and changed // 2 to @Id) it works.
According to the JPA documentation it should work the same way (at least I expected).
My environment: PostgreSQL 9, Spring framework 3.1, Hibernate 3.6.8.Final, Tomcat 7 (JPA support is added by Spring instrumentation).
Update: I've tried both mappings with EclipseLink and it worked. So the problem is somewhere with Hibernate, probably. Still have no idea how I can force it to work with Hibernate.