Skip to content

Commit

Permalink
Re-enabled additional tests and fix issues with IdClass
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Nov 25, 2020
1 parent 52ba3d9 commit e3947b3
Show file tree
Hide file tree
Showing 97 changed files with 1,261 additions and 1,296 deletions.
Expand Up @@ -7,17 +7,12 @@
package org.hibernate.userguide.pc;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Target;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;

import org.hibernate.test.annotations.id.entities.Shoe;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
Expand Down
Expand Up @@ -153,10 +153,10 @@ public static CompositeIdentifierMapping buildNonEncapsulatedCompositeIdentifier
PersistentClass bootEntityDescriptor,
BiConsumer<String,SingularAttributeMapping> idSubAttributeConsumer,
MappingModelCreationProcess creationProcess) {
final Component bootCompositeDescriptor = (Component) bootEntityDescriptor.getIdentifier();
final Component bootIdClassComponent = (Component) bootEntityDescriptor.getIdentifier();

final EmbeddableMappingType embeddableMappingType = EmbeddableMappingType.from(
bootCompositeDescriptor,
bootIdClassComponent,
cidType,
attributeMappingType -> {
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
Expand All @@ -167,15 +167,19 @@ public static CompositeIdentifierMapping buildNonEncapsulatedCompositeIdentifier
final StateArrayContributorMetadataAccess attributeMetadataAccess = getStateArrayContributorMetadataAccess(
propertyAccess
);
final Component idClass = bootEntityDescriptor.getIdentifierMapper();
Component bootIdDescriptor = bootEntityDescriptor.getDeclaredIdentifierMapper();
if ( bootIdDescriptor == null ) {
bootIdDescriptor = (Component) bootEntityDescriptor.getIdentifier();
Component bootComponentDescriptor = bootEntityDescriptor.getIdentifierMapper();
final List<SingularAttributeMapping> idAttributeMappings;
final Iterator<Property> bootIdSubPropertyItr;
if ( bootComponentDescriptor == null ) {
idAttributeMappings = new ArrayList<>( bootIdClassComponent.getPropertySpan() );
bootIdSubPropertyItr = bootIdClassComponent.getPropertyIterator();

}
else {
idAttributeMappings = new ArrayList<>( bootComponentDescriptor.getPropertySpan() );
bootIdSubPropertyItr = bootComponentDescriptor.getPropertyIterator();
}
final List<SingularAttributeMapping> idAttributeMappings = new ArrayList<>( bootIdDescriptor.getPropertySpan() );

//noinspection unchecked
final Iterator<Property> bootIdSubPropertyItr = bootIdDescriptor.getPropertyIterator();
int columnsConsumedSoFar = 0;

while ( bootIdSubPropertyItr.hasNext() ) {
Expand Down Expand Up @@ -245,7 +249,7 @@ else if ( idSubPropertyType instanceof EntityType ) {
}

idAttributeMappings.add( idSubAttribute );
if ( idClass == null ) {
if ( bootComponentDescriptor == null ) {
idSubAttributeConsumer.accept( idSubAttribute.getAttributeName(), idSubAttribute );
}
}
Expand All @@ -257,8 +261,8 @@ else if ( idSubPropertyType instanceof EntityType ) {
attributeMetadataAccess,
rootTableName,
rootTableKeyColumnNames,
bootIdDescriptor,
bootCompositeDescriptor,
bootIdClassComponent,
bootComponentDescriptor,
creationProcess
);
},
Expand Down
Expand Up @@ -48,8 +48,8 @@ public NonAggregatedIdentifierMappingImpl(
StateArrayContributorMetadataAccess attributeMetadataAccess,
String rootTableName,
String[] rootTableKeyColumnNames,
Component bootCidDescriptor,
Component bootIdClassDescriptor,
Component bootCidDescriptor,
MappingModelCreationProcess creationProcess) {
// todo (6.0) : handle MapsId
super(
Expand Down Expand Up @@ -78,9 +78,6 @@ public Collection<SingularAttributeMapping> getAttributes() {

@Override
public Object getIdentifier(Object entity, SharedSessionContractImplementor session) {
if ( entity instanceof HibernateProxy ) {
return ( (HibernateProxy) entity ).getHibernateLazyInitializer().getIdentifier();
}
final Serializable disassemble = bootCidDescriptor.getType().disassemble( entity, session, null );
return bootIdClassDescriptor.getType().assemble( disassemble, session, null );
}
Expand Down
Expand Up @@ -346,6 +346,11 @@ public void applyNonAggregatedIdAttributes(Set idAttributes) {
}
}

@Override
public void applyIdClassAttributes(Set idClassAttributes) {
applyNonAggregatedIdAttributes( idClassAttributes );
}

@Override
@SuppressWarnings("unchecked")
public void applyVersionAttribute(SingularPersistentAttribute versionAttribute) {
Expand Down
Expand Up @@ -4,7 +4,7 @@
* 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.annotations.id;
package org.hibernate.orm.test.annotations.id;

import javax.persistence.Entity;
import javax.persistence.Id;
Expand All @@ -15,30 +15,33 @@
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.CannotForceNonNullableException;

import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.hibernate.testing.junit5.BaseUnitTest;
import org.hibernate.testing.orm.junit.ServiceRegistry;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Originally developed for HHH-9807 - better error message on combination of {@code @Id} + {@code @Formula}
*
* @author Steve Ebersole
*/
public class AndFormulaTest extends BaseUnitTestCase {
@ServiceRegistry
public class AndFormulaTest extends BaseUnitTest {
private static StandardServiceRegistry ssr;

@BeforeClass
public static void prepareServiceRegistry() {
@BeforeEach
public void prepareServiceRegistry() {
ssr = new StandardServiceRegistryBuilder().build();
}

@AfterClass
public static void releaseServiceRegistry() {
@AfterEach
public void releaseServiceRegistry() {
if ( ssr != null ) {
StandardServiceRegistryBuilder.destroy( ssr );
}
Expand Down
@@ -0,0 +1,66 @@
/*
* 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.orm.test.annotations.id;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.orm.test.annotations.id.entities.Planet;
import org.hibernate.orm.test.annotations.id.entities.PlanetCheatSheet;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;


/**
* Tests for enum type as id.
*
* @author Hardy Ferentschik
*/
@TestForIssue(jiraKey = "ANN-744")
@DomainModel(
annotatedClasses = PlanetCheatSheet.class
)
@SessionFactory
public class EnumIdTest {

@Test
public void testEnumAsId(SessionFactoryScope scope) {
PlanetCheatSheet mercury = new PlanetCheatSheet();
scope.inTransaction(
session -> {
mercury.setPlanet( Planet.MERCURY );
mercury.setMass( 3.303e+23 );
mercury.setRadius( 2.4397e6 );
mercury.setNumberOfInhabitants( 0 );
session.persist( mercury );
}
);

scope.inTransaction(
session -> {
PlanetCheatSheet mercuryFromDb = session.get( PlanetCheatSheet.class, mercury.getPlanet() );
assertNotNull( mercuryFromDb );
session.delete( mercuryFromDb );
}
);

scope.inTransaction(
session -> {
PlanetCheatSheet mercuryFromDb = session.get(
PlanetCheatSheet.class,
Planet.MERCURY
);
assertNull( mercuryFromDb );
}
);

}

}
@@ -0,0 +1,47 @@
/*
* 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.orm.test.annotations.id;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.orm.test.annotations.id.entities.Location;
import org.hibernate.orm.test.annotations.id.entities.Tower;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;


/**
* @author Emmanuel Bernard
*/
@DomainModel(
annotatedClasses = Tower.class
)
@SessionFactory
public class IdClassTest {

@Test
public void testIdClassInSuperclass(SessionFactoryScope scope) {
Tower tower = new Tower();
tower.latitude = 10.3;
tower.longitude = 45.4;

scope.inTransaction(
session -> {
session.persist( tower );
session.flush();
session.clear();
Location loc = new Location();
loc.latitude = tower.latitude;
loc.longitude = tower.longitude;
assertNotNull( session.get( Tower.class, loc ) );
}
);
}

}

0 comments on commit e3947b3

Please sign in to comment.