HHH-11924: ElementCollection ignore converter for XML mapping

This commit is contained in:
Lars Storm 2018-03-05 13:39:35 +01:00 committed by Vlad Mihalcea
parent 65f7dc1b60
commit 1009245de5
7 changed files with 181 additions and 69 deletions

View File

@ -437,10 +437,9 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
for ( Element element : elementsForProperty ) {
final boolean isBasic = "basic".equals( element.getName() );
final boolean isEmbedded = "embedded".equals( element.getName() );
final boolean isElementCollection = "element-collection".equals(element.getName());
// todo : can be collections too
final boolean canHaveConverts = isBasic || isEmbedded;
final boolean canHaveConverts = isBasic || isEmbedded || isElementCollection;
if ( !canHaveConverts ) {
continue;

View File

@ -0,0 +1,36 @@
/*
* 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.annotations.reflection;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Vlad Mihalcea
*/
@Entity
public class Company {
@Id
private Long id;
private List<Organization> organizations = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Organization> getOrganizations() {
return organizations;
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.annotations.reflection;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
@TestForIssue( jiraKey = "HHH-11924")
public class ElementCollectionConverterTest extends BaseCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
Company.class,
};
}
@Override
protected String[] getXmlFiles() {
return new String[] { "org/hibernate/test/annotations/reflection/element-collection-converter-orm.xml" };
}
@Test
public void testConverterIsAppliedToElementCollection() {
doInHibernate( this::sessionFactory, session -> {
Company company = new Company();
company.setId( 1L );
Organization org1 = new Organization();
org1.setOrganizationId( "ACME" );
company.getOrganizations().add( org1 );
session.persist( company );
} );
doInHibernate( this::sessionFactory, session -> {
String organizationId = (String) session
.createNativeQuery( "select organizations from Company_organizations" )
.getSingleResult();
assertEquals( "ORG-ACME", organizationId );
Company company = session.find( Company.class, 1L );
assertEquals( 1, company.getOrganizations().size() );
assertEquals( "ACME" , company.getOrganizations().get( 0 ).getOrganizationId());
} );
}
}

View File

@ -6,82 +6,31 @@
*/
package org.hibernate.test.annotations.reflection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverrides;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ExcludeDefaultListeners;
import javax.persistence.ExcludeSuperclassListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.MappedSuperclass;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedQueries;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.PrimaryKeyJoinColumns;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import javax.persistence.SequenceGenerator;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.annotations.Columns;
import org.hibernate.cfg.EJB3DTDEntityResolver;
import org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader;
import org.hibernate.cfg.annotations.reflection.XMLContext;
import org.hibernate.internal.util.xml.ErrorLogger;
import org.hibernate.internal.util.xml.XMLHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.boot.ClassLoaderAccessTestingImpl;
import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotSupportedException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import javax.persistence.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.junit.Assert.*;
/**
* @author Emmanuel Bernard
@ -403,6 +352,20 @@ public class JPAOverriddenAnnotationReaderTest extends BaseUnitTestCase {
assertEquals( "maxSpeed", reader.getAnnotation( OrderBy.class ).value() );
}
@Test
@TestForIssue(jiraKey = "HHH-11924")
public void testElementCollectionConverter() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Field field = Company.class.getDeclaredField( "organizations" );
JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader( field, context, ClassLoaderAccessTestingImpl.INSTANCE );
assertNotNull( reader.getAnnotation( ElementCollection.class ) );
assertNotNull( reader.getAnnotation( Converts.class ) );
assertNotNull( reader.getAnnotation( Converts.class ).value() );
assertTrue( reader.getAnnotation( Converts.class ).value().length == 1 );
assertEquals(OrganizationConverter.class, reader.getAnnotation( Converts.class ).value()[0].converter());
}
@Test
public void testEntityListeners() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );

View File

@ -0,0 +1,27 @@
/*
* 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.annotations.reflection;
import javax.persistence.AttributeConverter;
/**
* Converts {@link Organization} <=> String
*/
public class OrganizationConverter implements AttributeConverter<Organization, String> {
@Override
public String convertToDatabaseColumn(Organization organization) {
return "ORG-" + organization.getOrganizationId();
}
@Override
public Organization convertToEntityAttribute(String organizationId) {
Organization organization = new Organization();
organization.setOrganizationId(organizationId.replace("ORG-", ""));
return organization;
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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>.
-->
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm orm_2_1.xsd"
version="2.1"
>
<package>org.hibernate.test.annotations.reflection</package>
<entity class="Company">
<attributes>
<element-collection name="organizations">
<convert converter="org.hibernate.test.annotations.reflection.OrganizationConverter" />
</element-collection>
</attributes>
</entity>
</entity-mappings>

View File

@ -6,10 +6,10 @@
~ 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>.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm orm_2_1.xsd"
version="2.1"
>
<persistence-unit-metadata>
<persistence-unit-defaults>
@ -120,4 +120,11 @@
<discriminator-value>Physical</discriminator-value>
<discriminator-column length="34"/>
</entity>
<entity class="Company">
<attributes>
<element-collection name="organizations">
<convert converter="org.hibernate.test.annotations.reflection.OrganizationConverter" />
</element-collection>
</attributes>
</entity>
</entity-mappings>