I'm having trouble getting the @ManyToOne association loaded by lazilly. I use fetch = LAZY, but it does not work when the connection is not made in the primary key column.
I know that this question has already been asked , but I think I have not answered it properly, so I provide detailed information to clarify the problem.
This is my model:
DummyB -> DummyA
These are the tables:
create table dummyA ( id number(18,0),
And these are the entities:
@Entity public class DummyA implements Serializable { private Long id; private String name; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Entity public class DummyB implements Serializable { private Long id; private DummyA dummyA; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; }
Note. The getDummyA method in the DummyB entity is a duplicate to try two cases for combining objects.
Case 1: mapping DummyB -> DummyA using the primary key DummyA
@ManyToOne (fetch = FetchType.LAZY) @JoinColumn (name = "dummya_id")
This works fine, only one query is executed to retrieve DummyB objects.
Case 2: mapping DummyB → DummyA by DummyA NON primary key (field name)
@ManyToOne (fetch = FetchType.LAZY) @JoinColumn (name = "dummya_name", referenceColumnName = "name")
The same dummy choiceB is executed, but immediately after choosing dummyA, filtering is performed using name =? to get related object A.
I use very simple jUnit to do the filtering:
public class DummyTest { @Autowired HibernateTransactionManager transactionManager; @Test @Transactional public void testFindDummyB() throws DAOException { Long idDummyB = 2L; Session session = getCurrentHibernateSession(); List lst = session.createCriteria(DummyB.class) .add(Restrictions.eq("id", idDummyB)).list(); assertTrue(lst.size() > 0); } private Session getCurrentHibernateSession() { return this.transactionManager.getSessionFactory().getCurrentSession(); } }
My libraries:
- org.hibernate: core hibernation: jar: 4.2.17.Final: assemble
- org.hibernate.common: sleeping-Wikimedia Commons annotations: jar: 4.0.2. Final: compile
- org.hibernate.javax.persistence: hibernation-JPA-2.0-API: bank: 1.0.1.Final: collect
- org.hibernate: hibernate validator: jar: 4.3.2.Final: provided
Other things I've already tried:
Adding the hiberante @LazyToOne method to getDummyA () has no effect.
@LazyToOne (LazyToOneOption.PROXY) @ManyToOne (fetch = FetchType.LAZY, optional = true) @JoinColumn (name = "dummya_name", linkColumnName = "name") @LazyToOne (LazyToOYOption)
Creating a foreign key from the DummyB table in dummyA (and the unique constraint in the dummya.name field) has no effect.
- Adding @Column (unique = true) to the DummyA method getName () did not.
- Set the option = true or false as suggested here .
Trying to force lazy loading to use setFetchMode in the criteria does not work, DummyA selection continues to be executed.
List lst = session.createCriteria (DummyB.class) .add (Restrictions.eq ("id", idDummyB)). setFetchMode ("dummyA", FetchMode.SELECT) .list ();
I can’t find the point at which it refers to this behavior in the Hibernate docs, so I wonder if something is wrong in my annotations or I encountered a Hibernate error.
Can anyone tell?
UPDATED by md-dev request: To make this more clear:
Is this the expected behavior or is it a mistake? if this is the expected behavior, where is it documented?
Thanks.