mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-22 02:58:05 +00:00
HHH-12085 - @NaturalId not working on inherited field
This commit is contained in:
parent
f92bffae43
commit
e9263758fe
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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.naturalid.inheritance;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.annotations.NaturalIdCache;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-12085" )
|
||||
public class MappedSuperclassOverrideTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
createTestData();
|
||||
|
||||
try {
|
||||
inTransaction(
|
||||
session -> session.createQuery( "select e from MyEntity e" ).list()
|
||||
);
|
||||
}
|
||||
finally {
|
||||
dropTestData();
|
||||
}
|
||||
}
|
||||
|
||||
private void createTestData() {
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.save( new MyEntity( 1, "first" ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void dropTestData() {
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.createQuery( "delete MyEntity" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
super.addSettings( settings );
|
||||
settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyMetadataSources(MetadataSources metadataSources) {
|
||||
super.applyMetadataSources( metadataSources );
|
||||
metadataSources.addAnnotatedClass( MyMappedSuperclass.class )
|
||||
.addAnnotatedClass( MyEntity.class );
|
||||
}
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract static class MyMappedSuperclass {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public MyMappedSuperclass() {
|
||||
}
|
||||
|
||||
public MyMappedSuperclass(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
protected void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity( name = "MyEntity" )
|
||||
@Table( name = "the_entity" )
|
||||
@NaturalIdCache
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
|
||||
public static class MyEntity extends MyMappedSuperclass {
|
||||
public MyEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyEntity(Integer id, String name) {
|
||||
super( id, name );
|
||||
}
|
||||
|
||||
// this should not be allowed, and supposedly fails anyway...
|
||||
@Override
|
||||
public String getName() {
|
||||
return super.getName();
|
||||
}
|
||||
}
|
||||
}
|
@ -15,10 +15,12 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Interceptor;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataBuilder;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
@ -542,4 +544,77 @@ protected void assertAllDataRemoved() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void inSession(Consumer<SessionImplementor> action) {
|
||||
log.trace( "#inSession(action)" );
|
||||
inSession( sessionFactory(), action );
|
||||
}
|
||||
|
||||
public void inTransaction(Consumer<SessionImplementor> action) {
|
||||
log.trace( "#inTransaction(action)" );
|
||||
inTransaction( sessionFactory(), action );
|
||||
}
|
||||
|
||||
public void inSession(SessionFactoryImplementor sfi, Consumer<SessionImplementor> action) {
|
||||
log.trace( "##inSession(SF,action)" );
|
||||
|
||||
try (SessionImplementor session = (SessionImplementor) sfi.openSession()) {
|
||||
log.trace( "Session opened, calling action" );
|
||||
action.accept( session );
|
||||
log.trace( "called action" );
|
||||
}
|
||||
finally {
|
||||
log.trace( "Session close - auto-close lock" );
|
||||
}
|
||||
}
|
||||
|
||||
public void inTransaction(SessionFactoryImplementor factory, Consumer<SessionImplementor> action) {
|
||||
log.trace( "#inTransaction(factory, action)");
|
||||
|
||||
|
||||
try (SessionImplementor session = (SessionImplementor) factory.openSession()) {
|
||||
log.trace( "Session opened, calling action" );
|
||||
inTransaction( session, action );
|
||||
log.trace( "called action" );
|
||||
}
|
||||
finally {
|
||||
log.trace( "Session close - auto-close lock" );
|
||||
}
|
||||
}
|
||||
|
||||
public void inTransaction(SessionImplementor session, Consumer<SessionImplementor> action) {
|
||||
log.trace( "inTransaction(session,action)" );
|
||||
|
||||
final Transaction txn = session.beginTransaction();
|
||||
log.trace( "Started transaction" );
|
||||
|
||||
try {
|
||||
log.trace( "Calling action in txn" );
|
||||
action.accept( session );
|
||||
log.trace( "Called action - in txn" );
|
||||
|
||||
log.trace( "Committing transaction" );
|
||||
txn.commit();
|
||||
log.trace( "Committed transaction" );
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.tracef(
|
||||
"Error calling action: %s (%s) - rolling back",
|
||||
e.getClass().getName(),
|
||||
e.getMessage()
|
||||
);
|
||||
try {
|
||||
txn.rollback();
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
log.trace( "Was unable to roll back transaction" );
|
||||
// really nothing else we can do here - the attempt to
|
||||
// rollback already failed and there is nothing else
|
||||
// to clean up.
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user