To be more concrete here is my example, going against Apache Derby.
Code:
package hello;
import javax.persistence.*;
@Entity
public class InsurancePlan {
@Id @GeneratedValue
private Long id;
private String name;
@ManyToOne(fetch=FetchType.EAGER, optional = false)
@JoinColumn(name = "plan_type_id")
private InsurancePlanType planType;
@SuppressWarnings("unused")
private InsurancePlan() { }
public InsurancePlan(String name, InsurancePlanType planType) {
this.name = name;
this.planType = planType;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public InsurancePlanType getInsurancePlanType() {
return planType;
}
}
and
Code:
package hello;
import javax.persistence.*;
@Entity
public class InsurancePlanType {
@Id @GeneratedValue
private Long id;
private String name;
@SuppressWarnings("unused")
private InsurancePlanType() {}
public InsurancePlanType(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
}
This test
Code:
package hello;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
public class InsurancePlanTest {
private static EntityManagerFactory entityManagerFactory;
private EntityManager em;
private List<InsurancePlan> plans;
@BeforeClass
public static void createPersistence() {
entityManagerFactory = Persistence.createEntityManagerFactory("helloworld");
}
@SuppressWarnings("unchecked")
@Test
public void createAndSearch() {
em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
InsurancePlanType planTypeA = new InsurancePlanType("A plan type");
InsurancePlanType planTypeB = new InsurancePlanType("B plan type");
em.persist(planTypeA);
em.persist(planTypeB);
em.persist(new InsurancePlan("1 plan", planTypeA));
em.persist(new InsurancePlan("2 plan", planTypeA));
em.persist(new InsurancePlan("3 plan", planTypeB));
em.flush();
em.getTransaction().commit();
em.close();
em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
plans = em.createQuery("select p from InsurancePlan p").getResultList();
for (InsurancePlan plan : plans) {
System.out.println(plan.getName());
System.out.println(plan.getInsurancePlanType().getName());
}
em.getTransaction().commit();
}
}
results in these queries: (notice the multiple selects against the "one" table).
Code:
Hibernate:
insert
into
insurance_plan_type
(name, id)
values
(?, ?)
Hibernate:
insert
into
insurance_plan_type
(name, id)
values
(?, ?)
Hibernate:
insert
into
insurance_plan
(name, plan_type_id, id)
values
(?, ?, ?)
Hibernate:
insert
into
insurance_plan
(name, plan_type_id, id)
values
(?, ?, ?)
Hibernate:
insert
into
insurance_plan
(name, plan_type_id, id)
values
(?, ?, ?)
Hibernate:
select
insurancep0_.id as id0_,
insurancep0_.name as name0_,
insurancep0_.plan_type_id as plan3_0_
from
insurance_plan insurancep0_
Hibernate:
select
insurancep0_.id as id2_0_,
insurancep0_.name as name2_0_
from
insurance_plan_type insurancep0_
where
insurancep0_.id=?
Hibernate:
select
insurancep0_.id as id2_0_,
insurancep0_.name as name2_0_
from
insurance_plan_type insurancep0_
where
insurancep0_.id=?
Is this done to make sure that there is only instance of each item in the one side of the relationship? It seems it would be more efficient to just go ahead an do a join and toss out the duplicates from the one side.