|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.mapping.naturalid; |
| 8 | + |
| 9 | +import org.hibernate.MappingException; |
| 10 | +import org.hibernate.SessionFactory; |
| 11 | +import org.hibernate.annotations.NaturalId; |
| 12 | +import org.hibernate.annotations.NotFound; |
| 13 | +import org.hibernate.annotations.NotFoundAction; |
| 14 | +import org.hibernate.boot.MetadataSources; |
| 15 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 16 | + |
| 17 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 18 | +import org.hibernate.testing.orm.junit.ServiceRegistryScope; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import jakarta.persistence.Embeddable; |
| 22 | +import jakarta.persistence.Embedded; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.ManyToOne; |
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.junit.jupiter.api.Assertions.fail; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Steve Ebersole |
| 33 | + */ |
| 34 | +@ServiceRegistry |
| 35 | +public class ValidationTests { |
| 36 | + @Test |
| 37 | + void checkManyToOne(ServiceRegistryScope registryScope) { |
| 38 | + final StandardServiceRegistry registry = registryScope.getRegistry(); |
| 39 | + final MetadataSources metadataSources = new MetadataSources( registry ) |
| 40 | + .addAnnotatedClass( Thing1.class ) |
| 41 | + .addAnnotatedClass( Thing2.class ); |
| 42 | + try (final SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); ) { |
| 43 | + fail( "Expecting an exception" ); |
| 44 | + } |
| 45 | + catch (MappingException expected) { |
| 46 | + assertThat( expected.getMessage() ) |
| 47 | + .startsWith( "Attribute marked as natural-id can not also be a not-found association - " ); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void checkEmbeddable(ServiceRegistryScope registryScope) { |
| 53 | + final StandardServiceRegistry registry = registryScope.getRegistry(); |
| 54 | + final MetadataSources metadataSources = new MetadataSources( registry ) |
| 55 | + .addAnnotatedClass( Thing1.class ) |
| 56 | + .addAnnotatedClass( Thing3.class ) |
| 57 | + .addAnnotatedClass( Container.class ); |
| 58 | + try (final SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); ) { |
| 59 | + fail( "Expecting an exception" ); |
| 60 | + } |
| 61 | + catch (MappingException expected) { |
| 62 | + assertThat( expected.getMessage() ) |
| 63 | + .startsWith( "Attribute marked as natural-id can not also be a not-found association - " ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Entity(name="Thing1") |
| 68 | + @Table(name="thing_1") |
| 69 | + public static class Thing1 { |
| 70 | + @Id |
| 71 | + private Integer id; |
| 72 | + private String name; |
| 73 | + } |
| 74 | + |
| 75 | + @Entity(name="Thing2") |
| 76 | + @Table(name="thing_2") |
| 77 | + public static class Thing2 { |
| 78 | + @Id |
| 79 | + private Integer id; |
| 80 | + private String name; |
| 81 | + @NaturalId |
| 82 | + @ManyToOne |
| 83 | + @NotFound(action = NotFoundAction.IGNORE) |
| 84 | + private Thing1 thing1; |
| 85 | + } |
| 86 | + |
| 87 | + @Embeddable |
| 88 | + public static class Container { |
| 89 | + @NaturalId |
| 90 | + @ManyToOne |
| 91 | + @NotFound(action = NotFoundAction.IGNORE) |
| 92 | + private Thing1 thing1; |
| 93 | + } |
| 94 | + |
| 95 | + @Entity(name="Thing2") |
| 96 | + @Table(name="thing_2") |
| 97 | + public static class Thing3 { |
| 98 | + @Id |
| 99 | + private Integer id; |
| 100 | + private String name; |
| 101 | + @NaturalId |
| 102 | + @Embedded |
| 103 | + private Container container; |
| 104 | + } |
| 105 | +} |
0 commit comments