Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-11703 - Entity with Natural ID not being cached in the persistenc…
…eContext, causing extra queries
- Loading branch information
1 parent
ef8ebe0
commit 1c34914
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
hibernate-core/src/test/java/org/hibernate/test/uniquekey/NaturalIdCachingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.test.uniquekey; | ||
|
||
import java.io.Serializable; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinColumns; | ||
import javax.persistence.ManyToOne; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import org.hibernate.annotations.NaturalId; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.cfg.Configuration; | ||
import org.junit.Test; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
|
||
public class NaturalIdCachingTest extends BaseCoreFunctionalTestCase { | ||
|
||
@Override | ||
protected Class[] getAnnotatedClasses() { | ||
return new Class[]{ | ||
PropertyHolder.class, | ||
Property.class | ||
}; | ||
} | ||
|
||
@Override | ||
protected void configure(Configuration configuration) { | ||
super.configure(configuration); | ||
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString()); | ||
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true"); | ||
} | ||
|
||
|
||
@Test | ||
public void test() throws Exception { | ||
doInHibernate( this::sessionFactory, session -> { | ||
Property property = new Property( 1, 1, 1 ); | ||
session.persist( property ); | ||
session.persist( new PropertyHolder( 1, property ) ); | ||
session.persist( new PropertyHolder( 2, property ) ); | ||
} ); | ||
|
||
assertThat(sessionFactory().getStatistics().getEntityInsertCount(), is(3L)); | ||
sessionFactory().getStatistics().clear(); | ||
|
||
doInHibernate( this::sessionFactory, session -> { | ||
session.byId( PropertyHolder.class ).load( 1 ); | ||
session.byId( PropertyHolder.class ).load( 2 ); | ||
} ); | ||
|
||
assertThat( sessionFactory().getStatistics().getEntityLoadCount(), is(3L) ); | ||
assertThat( sessionFactory().getStatistics().getPrepareStatementCount(), is(3L) ); | ||
} | ||
|
||
@Entity(name = "PropertyHolder") | ||
public static class PropertyHolder implements Serializable { | ||
|
||
@Id | ||
private Integer id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumns({ | ||
@JoinColumn(name="PROP_CODE", referencedColumnName = "CODE"), | ||
@JoinColumn(name="PROP_ITEM", referencedColumnName = "ITEM") | ||
}) | ||
private Property property; | ||
|
||
private String severalOtherFields = "Several other fields ..."; | ||
|
||
protected PropertyHolder() {} | ||
|
||
public PropertyHolder(Integer id, Property property) { | ||
this.id = id; | ||
this.property = property; | ||
} | ||
|
||
} | ||
|
||
@Entity(name = "PropertyEntity") | ||
public static class Property implements Serializable { | ||
|
||
@Id | ||
private Integer id; | ||
|
||
@NaturalId | ||
private Integer code; | ||
|
||
@NaturalId | ||
private Integer item; | ||
|
||
private String description = "A description ..."; | ||
|
||
protected Property(){} | ||
|
||
public Property(Integer id, Integer code, Integer item) { | ||
this.id = id; | ||
this.code = code; | ||
this.item = item; | ||
} | ||
} | ||
} |