| 
 | 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.embeddable;  | 
 | 6 | + | 
 | 7 | +import jakarta.persistence.Column;  | 
 | 8 | +import jakarta.persistence.Embeddable;  | 
 | 9 | +import jakarta.persistence.Embedded;  | 
 | 10 | +import jakarta.persistence.Entity;  | 
 | 11 | +import jakarta.persistence.Id;  | 
 | 12 | +import jakarta.persistence.Table;  | 
 | 13 | +import org.hibernate.MappingException;  | 
 | 14 | +import org.hibernate.annotations.EmbeddedColumnNaming;  | 
 | 15 | +import org.hibernate.engine.spi.SessionFactoryImplementor;  | 
 | 16 | +import org.hibernate.metamodel.spi.MappingMetamodelImplementor;  | 
 | 17 | +import org.hibernate.persister.entity.EntityPersister;  | 
 | 18 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 19 | +import org.hibernate.testing.orm.junit.NotImplementedYet;  | 
 | 20 | +import org.hibernate.testing.orm.junit.ServiceRegistry;  | 
 | 21 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 22 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 23 | +import org.junit.jupiter.api.Test;  | 
 | 24 | + | 
 | 25 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 26 | +import static org.hibernate.orm.test.embeddable.EmbeddedColumnNamingTests.verifyColumnNames;  | 
 | 27 | +import static org.junit.jupiter.api.Assertions.fail;  | 
 | 28 | + | 
 | 29 | +/**  | 
 | 30 | + * @author Steve Ebersole  | 
 | 31 | + */  | 
 | 32 | +@SuppressWarnings("JUnitMalformedDeclaration")  | 
 | 33 | +public class EmbeddedColumnNamingMixedTests {  | 
 | 34 | + | 
 | 35 | +	/**  | 
 | 36 | +	 * Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes with  | 
 | 37 | +	 * a nested embeddable using explicit column names  | 
 | 38 | +	 */  | 
 | 39 | +	@Test  | 
 | 40 | +	@ServiceRegistry  | 
 | 41 | +	@DomainModel(annotatedClasses = {Address.class, Person.class})  | 
 | 42 | +	@SessionFactory  | 
 | 43 | +	void smokeTest(SessionFactoryScope sfScope) {  | 
 | 44 | +		// atm this mixing is not supported / implemented - tbd  | 
 | 45 | +		try (SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory()) {  | 
 | 46 | +			fail( "Expecting an exception" );  | 
 | 47 | +		}  | 
 | 48 | +		catch (RuntimeException e) {  | 
 | 49 | +			assertThat( e ).hasCauseExactlyInstanceOf( MappingException.class );  | 
 | 50 | +			assertThat( e ).cause().hasMessageStartingWith( "Column 'zip_code' is duplicated in mapping for entity" );  | 
 | 51 | +		}  | 
 | 52 | +	}  | 
 | 53 | + | 
 | 54 | +	/**  | 
 | 55 | +	 * Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes with  | 
 | 56 | +	 * a nested embeddable using explicit column names  | 
 | 57 | +	 */  | 
 | 58 | +	@Test  | 
 | 59 | +	@ServiceRegistry  | 
 | 60 | +	@DomainModel(annotatedClasses = {Address.class, Person.class})  | 
 | 61 | +	@SessionFactory  | 
 | 62 | +	@NotImplementedYet( reason = "this mixing is not supported / implemented - tbd" )  | 
 | 63 | +	void testMixedNamingPattern(SessionFactoryScope sfScope) {  | 
 | 64 | +		final SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory();  | 
 | 65 | +		final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getMappingMetamodel();  | 
 | 66 | +		final EntityPersister persister = mappingMetamodel.getEntityDescriptor( Person.class );  | 
 | 67 | +		verifyColumnNames( persister.findAttributeMapping( "homeAddress" ), "home_" );  | 
 | 68 | +		verifyColumnNames( persister.findAttributeMapping( "workAddress" ), "work_" );  | 
 | 69 | +	}  | 
 | 70 | + | 
 | 71 | +	@Entity(name="Person")  | 
 | 72 | +	@Table(name="person")  | 
 | 73 | +	public static class Person {  | 
 | 74 | +		@Id  | 
 | 75 | +		private Integer id;  | 
 | 76 | +		private String name;  | 
 | 77 | + | 
 | 78 | +		@Embedded  | 
 | 79 | +		@EmbeddedColumnNaming("home_%s")  | 
 | 80 | +		private Address homeAddress;  | 
 | 81 | + | 
 | 82 | +		@Embedded  | 
 | 83 | +		@EmbeddedColumnNaming("work_%s")  | 
 | 84 | +		private Address workAddress;  | 
 | 85 | +	}  | 
 | 86 | + | 
 | 87 | +	@Embeddable  | 
 | 88 | +	public static class Address {  | 
 | 89 | +		private String street;  | 
 | 90 | +		private String city;  | 
 | 91 | +		private String state;  | 
 | 92 | +		@Embedded  | 
 | 93 | +		private ZipPlus zip;  | 
 | 94 | +	}  | 
 | 95 | + | 
 | 96 | +	@Embeddable  | 
 | 97 | +	public static class ZipPlus {  | 
 | 98 | +		@Column(name="zip_code")  | 
 | 99 | +		private String code;  | 
 | 100 | +		@Column(name="zip_plus4")  | 
 | 101 | +		private String plus4;  | 
 | 102 | +	}  | 
 | 103 | +}  | 
0 commit comments