|
6 | 6 | */
|
7 | 7 | package org.hibernate.orm.test.foreignkeys.disabled;
|
8 | 8 |
|
9 |
| -import static org.hamcrest.core.Is.is; |
10 |
| -import static org.junit.Assert.assertThat; |
11 |
| - |
| 9 | +import java.util.List; |
12 | 10 | import java.util.stream.StreamSupport;
|
13 | 11 |
|
14 |
| -import jakarta.persistence.Entity; |
15 |
| -import jakarta.persistence.Id; |
16 |
| -import jakarta.persistence.JoinColumn; |
17 |
| -import jakarta.persistence.ManyToOne; |
18 |
| - |
19 | 12 | import org.hibernate.boot.Metadata;
|
20 | 13 | import org.hibernate.boot.MetadataSources;
|
21 | 14 | import org.hibernate.boot.registry.StandardServiceRegistry;
|
22 | 15 | import org.hibernate.cfg.Environment;
|
23 | 16 | import org.hibernate.mapping.Table;
|
24 |
| -import org.hibernate.testing.TestForIssue; |
| 17 | + |
25 | 18 | import org.hibernate.testing.junit4.BaseUnitTestCase;
|
| 19 | +import org.hibernate.testing.orm.junit.Jira; |
26 | 20 | import org.hibernate.testing.util.ServiceRegistryUtil;
|
27 | 21 | import org.junit.Test;
|
28 | 22 |
|
| 23 | +import jakarta.persistence.ElementCollection; |
| 24 | +import jakarta.persistence.Entity; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.Inheritance; |
| 27 | +import jakarta.persistence.InheritanceType; |
| 28 | +import jakarta.persistence.JoinColumn; |
| 29 | +import jakarta.persistence.ManyToMany; |
| 30 | +import jakarta.persistence.ManyToOne; |
| 31 | +import jakarta.persistence.OneToMany; |
| 32 | + |
| 33 | +import static org.hamcrest.core.Is.is; |
| 34 | +import static org.junit.Assert.assertThat; |
| 35 | + |
29 | 36 | /**
|
30 | 37 | * @author Yanming Zhou
|
31 | 38 | */
|
| 39 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-14253" ) |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17550" ) |
32 | 41 | public class DefaultConstraintModeTest extends BaseUnitTestCase {
|
33 |
| - |
34 |
| - private static final String TABLE_NAME = "TestEntity"; |
35 |
| - |
36 | 42 | @Test
|
37 |
| - @TestForIssue(jiraKey = "HHH-14253") |
38 | 43 | public void testForeignKeyShouldNotBeCreated() {
|
39 |
| - testForeignKeyCreation(false); |
| 44 | + testForeignKeyCreation( false ); |
40 | 45 | }
|
41 | 46 |
|
42 | 47 | @Test
|
43 |
| - @TestForIssue(jiraKey = "HHH-14253") |
44 | 48 | public void testForeignKeyShouldBeCreated() {
|
45 |
| - testForeignKeyCreation(true); |
| 49 | + testForeignKeyCreation( true ); |
46 | 50 | }
|
47 | 51 |
|
48 | 52 | private void testForeignKeyCreation(boolean created) {
|
49 | 53 | try (StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder()
|
50 |
| - .applySetting(Environment.HBM2DDL_DEFAULT_CONSTRAINT_MODE, created ? "CONSTRAINT" : "NO_CONSTRAINT").build()) { |
51 |
| - Metadata metadata = new MetadataSources( ssr ).addAnnotatedClass( TestEntity.class ).buildMetadata(); |
52 |
| - assertThat( findTable( metadata, TABLE_NAME ).getForeignKeys().isEmpty(), is( !created ) ); |
| 54 | + .applySetting( Environment.HBM2DDL_DEFAULT_CONSTRAINT_MODE, created ? "CONSTRAINT" : "NO_CONSTRAINT" ) |
| 55 | + .build()) { |
| 56 | + Metadata metadata = new MetadataSources( ssr ).addAnnotatedClasses( TestEntity.class, ChildEntity.class ).buildMetadata(); |
| 57 | + assertThat( findTable( metadata, "TestEntity" ).getForeignKeys().isEmpty(), is( !created ) ); |
| 58 | + assertThat( findTable( metadata, "ChildEntity" ).getForeignKeys().isEmpty(), is( !created ) ); |
53 | 59 | }
|
54 | 60 | }
|
55 | 61 |
|
56 | 62 | private static Table findTable(Metadata metadata, String tableName) {
|
57 |
| - return StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false) |
58 |
| - .flatMap(namespace -> namespace.getTables().stream()).filter(t -> t.getName().equals(tableName)) |
59 |
| - .findFirst().orElse(null); |
| 63 | + return StreamSupport.stream( metadata.getDatabase().getNamespaces().spliterator(), false ) |
| 64 | + .flatMap( namespace -> namespace.getTables().stream() ).filter( t -> t.getName().equals( tableName ) ) |
| 65 | + .findFirst().orElse( null ); |
60 | 66 | }
|
61 | 67 |
|
62 |
| - @Entity |
63 |
| - @jakarta.persistence.Table(name = TABLE_NAME) |
| 68 | + @Entity( name = "TestEntity" ) |
| 69 | + @jakarta.persistence.Table( name = "TestEntity" ) |
| 70 | + @Inheritance( strategy = InheritanceType.JOINED ) |
64 | 71 | public static class TestEntity {
|
65 | 72 |
|
66 | 73 | @Id
|
67 | 74 | private Long id;
|
68 | 75 |
|
69 | 76 | @ManyToOne
|
| 77 | + private TestEntity toOne; |
| 78 | + |
| 79 | + @OneToMany |
70 | 80 | @JoinColumn
|
71 |
| - private TestEntity mate; |
| 81 | + private List<ChildEntity> oneToMany; |
| 82 | + |
| 83 | + @ManyToMany |
| 84 | + private List<ChildEntity> manyToMany; |
| 85 | + |
| 86 | + @ElementCollection |
| 87 | + private List<String> elements; |
| 88 | + } |
72 | 89 |
|
| 90 | + @Entity( name = "ChildEntity" ) |
| 91 | + @jakarta.persistence.Table( name = "ChildEntity" ) |
| 92 | + public static class ChildEntity extends TestEntity { |
| 93 | + private String childName; |
73 | 94 | }
|
74 | 95 | }
|
0 commit comments