|
| 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.test.schemaupdate; |
| 8 | + |
| 9 | +import javax.persistence.Column; |
| 10 | +import javax.persistence.Embeddable; |
| 11 | +import javax.persistence.EmbeddedId; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.ManyToOne; |
| 14 | +import javax.persistence.Table; |
| 15 | +import java.io.Serializable; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.apache.log4j.Logger; |
| 19 | + |
| 20 | +import org.hibernate.annotations.ForeignKey; |
| 21 | +import org.hibernate.boot.MetadataSources; |
| 22 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 23 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 24 | +import org.hibernate.tool.schema.internal.SchemaCreatorImpl; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.hibernate.testing.TestForIssue; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Andrea Boriero |
| 34 | + */ |
| 35 | +@TestForIssue(jiraKey = "HHH-9865") |
| 36 | +public class ImplicitCompositeKeyJoinTest { |
| 37 | + private static final Logger LOGGER = Logger.getLogger( ImplicitCompositeKeyJoinTest.class ); |
| 38 | + |
| 39 | + private final static String EXPECTED_SQL = "create table Employee " + |
| 40 | + "(age varchar(15) not null" + |
| 41 | + ", birthday varchar(255) not null" + |
| 42 | + ", name varchar(20) not null" + |
| 43 | + ", manager_age varchar(15)" + |
| 44 | + ", manager_birthday varchar(255)" + |
| 45 | + ", manager_name varchar(20)" + |
| 46 | + ", primary key (age, birthday, name))"; |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testImplicitCompositeJoin() throws Exception { |
| 50 | + |
| 51 | + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); |
| 52 | + try { |
| 53 | + org.hibernate.boot.Metadata metadata = new MetadataSources( ssr ) |
| 54 | + .addAnnotatedClass( Employee.class ) |
| 55 | + .buildMetadata(); |
| 56 | + |
| 57 | + boolean passed = false; |
| 58 | + |
| 59 | + List<String> commands = new SchemaCreatorImpl().generateCreationCommands( |
| 60 | + metadata, |
| 61 | + false |
| 62 | + ); |
| 63 | + for ( String command : commands ) { |
| 64 | + LOGGER.info( command ); |
| 65 | + |
| 66 | + if ( EXPECTED_SQL.equals( command ) ) { |
| 67 | + passed = true; |
| 68 | + } |
| 69 | + } |
| 70 | + assertTrue( |
| 71 | + "Expected create table command for Employee entity not found", |
| 72 | + passed |
| 73 | + ); |
| 74 | + } |
| 75 | + finally { |
| 76 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Entity |
| 81 | + @Table(name = "Employee") |
| 82 | + public class Employee { |
| 83 | + @EmbeddedId |
| 84 | + @ForeignKey(name = "none") |
| 85 | + private EmployeeId id; |
| 86 | + |
| 87 | + @ManyToOne(optional = true) |
| 88 | + @ForeignKey(name = "none") |
| 89 | + private Employee manager; |
| 90 | + |
| 91 | + public void setId(EmployeeId id) { |
| 92 | + this.id = id; |
| 93 | + } |
| 94 | + |
| 95 | + public EmployeeId getId() { |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + public void setManager(Employee manager) { |
| 100 | + this.manager = manager; |
| 101 | + } |
| 102 | + |
| 103 | + public Employee getManager() { |
| 104 | + return manager; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Embeddable |
| 109 | + public class EmployeeId implements Serializable { |
| 110 | + private static final long serialVersionUID = 1L; |
| 111 | + |
| 112 | + public EmployeeId(String name, String birthday, String age) { |
| 113 | + this.name = name; |
| 114 | + this.birthday = birthday; |
| 115 | + this.age = age; |
| 116 | + } |
| 117 | + |
| 118 | + @Column(length = 15) |
| 119 | + public String age; |
| 120 | + |
| 121 | + @Column(length = 20) |
| 122 | + private String name; |
| 123 | + |
| 124 | + private String birthday; |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + int hash = 1; |
| 129 | + hash = hash * 31 + (name != null ? name.hashCode() : 0); |
| 130 | + hash = hash * 31 + (age != null ? age.hashCode() : 0); |
| 131 | + return hash * 31 + (birthday != null ? birthday.hashCode() : 0); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean equals(Object obj) { |
| 136 | + if ( obj == this ) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + if ( !(obj instanceof EmployeeId) ) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + EmployeeId that = (EmployeeId) obj; |
| 144 | + if ( age != that.age ) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + if ( birthday != that.birthday ) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + if ( name != null && !name.equals( that.name ) ) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + public void setAge(String age) { |
| 157 | + this.age = age; |
| 158 | + } |
| 159 | + |
| 160 | + public void setName(String name) { |
| 161 | + this.name = name; |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + public void setBirthday(String birthday) { |
| 166 | + this.birthday = birthday; |
| 167 | + } |
| 168 | + |
| 169 | + public String getAge() { |
| 170 | + return age; |
| 171 | + } |
| 172 | + |
| 173 | + public String getName() { |
| 174 | + return name; |
| 175 | + } |
| 176 | + |
| 177 | + public String getBirthday() { |
| 178 | + return birthday; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments