| 
 | 1 | +/*  | 
 | 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later  | 
 | 3 | + * Copyright Red Hat Inc. and Hibernate Authors  | 
 | 4 | + */  | 
 | 5 | +package org.hibernate.orm.test.naming;  | 
 | 6 | + | 
 | 7 | +import jakarta.persistence.Embeddable;  | 
 | 8 | +import jakarta.persistence.Embedded;  | 
 | 9 | +import jakarta.persistence.Entity;  | 
 | 10 | +import jakarta.persistence.Id;  | 
 | 11 | +import jakarta.persistence.Table;  | 
 | 12 | +import org.hibernate.annotations.EmbeddedColumnNaming;  | 
 | 13 | +import org.hibernate.engine.spi.SessionFactoryImplementor;  | 
 | 14 | +import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;  | 
 | 15 | +import org.hibernate.metamodel.spi.MappingMetamodelImplementor;  | 
 | 16 | +import org.hibernate.persister.entity.EntityPersister;  | 
 | 17 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 18 | +import org.hibernate.testing.orm.junit.ServiceRegistry;  | 
 | 19 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 21 | +import org.junit.jupiter.api.Test;  | 
 | 22 | + | 
 | 23 | +import static org.hibernate.orm.test.naming.EmbeddedColumnNamingTests.verifyColumnNames;  | 
 | 24 | + | 
 | 25 | +/**  | 
 | 26 | + * @author Steve Ebersole  | 
 | 27 | + */  | 
 | 28 | +@SuppressWarnings("JUnitMalformedDeclaration")  | 
 | 29 | +public class NestedEmbeddableNamingTests {  | 
 | 30 | + | 
 | 31 | +	/**  | 
 | 32 | +	 * Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes  | 
 | 33 | +	 */  | 
 | 34 | +	@Test  | 
 | 35 | +	@ServiceRegistry  | 
 | 36 | +	@DomainModel(annotatedClasses = {Address.class, Person.class})  | 
 | 37 | +	@SessionFactory  | 
 | 38 | +	void testNestedNamingPattern(SessionFactoryScope sfScope) {  | 
 | 39 | +		final SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory();  | 
 | 40 | +		final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getMappingMetamodel();  | 
 | 41 | +		final EntityPersister persister = mappingMetamodel.getEntityDescriptor( Person.class );  | 
 | 42 | + | 
 | 43 | +		final EmbeddedAttributeMapping homeAddressMapping = (EmbeddedAttributeMapping) persister.findAttributeMapping( "homeAddress" );  | 
 | 44 | +		verifyColumnNames( homeAddressMapping, "home_" );  | 
 | 45 | +		final EmbeddedAttributeMapping homeZipMapping = (EmbeddedAttributeMapping) homeAddressMapping.getEmbeddableTypeDescriptor().findAttributeMapping( "zip" );  | 
 | 46 | +		verifyColumnNames( homeZipMapping, "home_zip" );  | 
 | 47 | + | 
 | 48 | +		final EmbeddedAttributeMapping workAddressMapping = (EmbeddedAttributeMapping) persister.findAttributeMapping( "workAddress" );  | 
 | 49 | +		verifyColumnNames( workAddressMapping, "work_" );  | 
 | 50 | +		final EmbeddedAttributeMapping workZipMapping = (EmbeddedAttributeMapping) workAddressMapping.getEmbeddableTypeDescriptor().findAttributeMapping( "zip" );  | 
 | 51 | +		verifyColumnNames( workZipMapping, "work_zip" );  | 
 | 52 | +	}  | 
 | 53 | + | 
 | 54 | +	@Entity(name="Person")  | 
 | 55 | +	@Table(name="person")  | 
 | 56 | +	public static class Person {  | 
 | 57 | +		@Id  | 
 | 58 | +		private Integer id;  | 
 | 59 | +		private String name;  | 
 | 60 | + | 
 | 61 | +		@Embedded  | 
 | 62 | +		@EmbeddedColumnNaming("home_%s")  | 
 | 63 | +		private Address homeAddress;  | 
 | 64 | + | 
 | 65 | +		@Embedded  | 
 | 66 | +		@EmbeddedColumnNaming("work_%s")  | 
 | 67 | +		private Address workAddress;  | 
 | 68 | +	}  | 
 | 69 | + | 
 | 70 | +	@Embeddable  | 
 | 71 | +	public static class Address {  | 
 | 72 | +		private String street;  | 
 | 73 | +		private String city;  | 
 | 74 | +		private String state;  | 
 | 75 | +		@Embedded  | 
 | 76 | +		@EmbeddedColumnNaming( "zip_%s" )  | 
 | 77 | +		private ZipPlus zip;  | 
 | 78 | +	}  | 
 | 79 | + | 
 | 80 | +	@Embeddable  | 
 | 81 | +	public static class ZipPlus {  | 
 | 82 | +		private String code;  | 
 | 83 | +		private String plus4;  | 
 | 84 | +	}  | 
 | 85 | +}  | 
0 commit comments