Thx @vlad for response.
I change to mappedBy and it works for primary key relation.
But I need join on non primary key column and one from primary key.
I use TENANT_ID and TAX_NUMBER instead of TENANT_ID and ID.
Example with exception:
package org.hibernate.bugs;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.hibernate.annotations.Formula;
import org.junit.Before;
import org.junit.Test;
public class NonPkCompositeKey {
private EntityManagerFactory templatePU;
@Before
public void init() {
templatePU = Persistence.createEntityManagerFactory("templatePU");
}
@Test
public void mappingTest() throws Exception {
final EntityManager entityManager = templatePU.createEntityManager();
final TypedQuery<EntityA> query = entityManager
.createQuery("from NonPkCompositeKey$EntityA a join fetch a.entityBSet", EntityA.class);
query.getResultList();
}
@Entity
public static class EntityA {
@EmbeddedId
private TenantId id;
@Column(name = "TAX_NUMBER")
private Long entityB_businessId;
@OneToMany(mappedBy = "entityA")
private Set<EntityB> entityBSet;
}
@Entity
public static class EntityB {
@EmbeddedId
private TenantId id;
@Column(name = "TAX_NUMBER")
private Long entityB_businessId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "TENANT_ID", referencedColumnName = "TENANT_ID",
insertable=false, updatable=false),
@JoinColumn(name = "TAX_NUMBER", referencedColumnName = "TAX_NUMBER",
insertable=false, updatable=false)
})
private EntityA entityA;
}
@Embeddable
public static class TenantId implements Serializable {
private Long id;
@Column(name = "TENANT_ID")
private Long tenantId;
}
}