Edit: Hibernate version 6.6.0.Final
I have a Store and a Region. A Region can have many stores and a Store can belong to one Region. My problem is that whenever I select a Store that has a region. A select statement fetching that Region is automatically created.
Here is my Store class:
@Entity
@Table(name="store")
public class Store {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "store_id", nullable = false)
private Long storeId;
@NaturalId
@Column(name = "store_code", length = 10, unique = true ,nullable = false)
private String storeCode;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name= "region_code", referencedColumnName = "region_code", foreignKey =
@ForeignKey(name="store_region"))
private Region region;
@Column(name = "region_code", updatable = false, insertable = false)
private String regionCode;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "address")
private String address;
@Column(name= "city")
private String city;
@Column(name= "state", nullable = false)
private String state;
//Getters and Setters omitted....
}
Here is my Region class
@Entity
@Table(name="region")
public class Region {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "region_id", nullable = false)
private Long regionId;
@OneToMany(mappedBy = Store_.REGION, fetch = FetchType.LAZY)
private Set<Store> stores;
@Column(name = "region_code", length = 10, unique = true ,nullable = false)
private String regionCode;
@Column(name = "region_name", unique = true ,nullable = false)
private String regionName;
//Getters and Setters omitted....
}
Here is a test case
class JPAUnitTestCase {
private EntityManagerFactory entityManagerFactory;
@BeforeEach
void init() {
entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" );
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
Region region = new Region("WEST", "WE");
Store store1 = new Store("1", "Billy's Hardware", "CA");
Store store2 = new Store("2", "Sandy's Restaurant", "CA");
Store store3 = new Store("3", "Dick's Hardware", "OH");
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
region = session.merge(region);
store1.setRegion(region);
store2.setRegion(region);
session.persist(store1);
session.persist(store2);
session.persist(store3);
t.commit();
session.close();
}
@AfterEach
void destroy() {
entityManagerFactory.close();
}
// Entities are auto-discovered, so just add them anywhere on class-path
// Add your tests, using standard JUnit.
@Test
void hhh123Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
Session session = sessionFactory.openSession();
List<Store> stores = session.createSelectionQuery("select store from Store store", Store.class).getResultList();
session.close();;
}
}
If you view the logs from the hhh123Test(), you can see two select statements for each of the regions being created. Any advice? I’m unsure of how to proceed from here