diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java index f5711ef8d6..92ca7eb057 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java @@ -52,7 +52,8 @@ public final class StringHelper { public static int lastIndexOfLetter(String string) { for ( int i=0; i tables = configuration().getTableMappings(); Table table1 = null; @@ -60,9 +72,48 @@ public class AliasTest extends BaseCoreFunctionalTestCase { assertTrue( table1.getUniqueInteger() < table2.getUniqueInteger() ); } + + @Test + @TestForIssue( jiraKey = "HHH-8371" ) + public final void testUnderscoreInColumnName() throws Throwable { + final Session s = openSession(); + s.getTransaction().begin(); + + UserEntity user = new UserEntity(); + user.setName( "foo" ); + s.persist(user); + final ConfEntity conf = new ConfEntity(); + conf.setConfKey("counter"); + conf.setConfValue("3"); + final UserConfEntity uc = new UserConfEntity(); + uc.setUser(user); + uc.setConf(conf); + conf.getUserConf().add(uc); + s.persist(conf); + + s.getTransaction().commit(); + s.clear(); + + s.getTransaction().begin(); + user = (UserEntity) s.get(UserEntity.class, user.getId()); + + try { + s.flush(); + } + catch ( HibernateException e ) { + // original issue from HHH-8371 + fail( "The explicit column name's underscore(s) were not considered during alias creation." ); + } + + assertNotNull( user ); + assertEquals( user.getName(), "foo" ); + + s.getTransaction().commit(); + s.close(); + } @Override protected Class[] getAnnotatedClasses() { - return new Class[] { Table1.class, Table2.class }; + return new Class[] { Table1.class, Table2.class, ConfEntity.class, UserConfEntity.class, UserEntity.class }; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfEntity.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfEntity.java new file mode 100644 index 0000000000..3b4a136f7c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfEntity.java @@ -0,0 +1,54 @@ +package org.hibernate.test.mapping; + +import static javax.persistence.CascadeType.ALL; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +@Entity +@Table(name = "CONF") +@IdClass(ConfId.class) +public class ConfEntity implements Serializable{ + + private static final long serialVersionUID = -5089484717715507169L; + + @Id + @Column(name = "confKey") + private String confKey; + + @Id + @Column(name = "confValue") + private String confValue; + + @OneToMany(mappedBy="conf", cascade = ALL, orphanRemoval = true, fetch = FetchType.LAZY) + private Set userConf = new HashSet(); + + public String getConfKey() { + return confKey; + } + + public void setConfKey(String confKey) { + this.confKey = confKey; + } + + public String getConfValue() { + return confValue; + } + + public void setConfValue(String confValue) { + this.confValue = confValue; + } + + public Set getUserConf() { + return userConf; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfId.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfId.java new file mode 100644 index 0000000000..72a95cd0fd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/ConfId.java @@ -0,0 +1,67 @@ +package org.hibernate.test.mapping; + +import java.io.Serializable; + +public class ConfId implements Serializable{ + + private static final long serialVersionUID = -6722022851594514199L; + + private String confKey; + + private String confValue; + + public ConfId(){ + } + + public ConfId(String confKey, String confValue) { + this.confKey = confKey; + this.confValue = confValue; + } + + public String getConfKey() { + return confKey; + } + + public void setConfKey(String confKey) { + this.confKey = confKey; + } + + public String getConfValue() { + return confValue; + } + + public void setConfValue(String confValue) { + this.confValue = confValue; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((confKey == null) ? 0 : confKey.hashCode()); + result = prime * result + ((confValue == null) ? 0 : confValue.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ConfId other = (ConfId) obj; + if (confKey == null) { + if (other.confKey != null) + return false; + } else if (!confKey.equals(other.confKey)) + return false; + else if (confValue == null) { + if (other.confValue != null) + return false; + } else if (!confValue.equals(other.confValue)) + return false; + return true; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfEntity.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfEntity.java new file mode 100644 index 0000000000..3e95a6bab1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfEntity.java @@ -0,0 +1,48 @@ +package org.hibernate.test.mapping; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "USER_CONFS") +@IdClass(UserConfId.class) +public class UserConfEntity implements Serializable{ + + private static final long serialVersionUID = 9153314908821604322L; + + @Id + @ManyToOne + @JoinColumn(name="user_id") + private UserEntity user; + + @Id + @ManyToOne + @JoinColumns({ + @JoinColumn(name="cnf_key", referencedColumnName="confKey"), + @JoinColumn(name="cnf_value", referencedColumnName="confValue")}) + private ConfEntity conf; + + public ConfEntity getConf() { + return conf; + } + + public void setConf(ConfEntity conf) { + this.conf = conf; + } + + + public UserEntity getUser() { + return user; + } + + public void setUser(UserEntity user) { + this.user = user; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfId.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfId.java new file mode 100644 index 0000000000..2965a95f06 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserConfId.java @@ -0,0 +1,70 @@ +package org.hibernate.test.mapping; + +import java.io.Serializable; + + + +public class UserConfId implements Serializable{ + + private static final long serialVersionUID = -161134972658451944L; + + private Long user; + + private ConfId conf; + + public UserConfId(){ + } + + public UserConfId(Long user, ConfId conf) { + this.user = user; + this.conf = conf; + } + + public Long getUser() { + return user; + } + + public void setUser(Long user) { + this.user = user; + } + + + public ConfId getConf() { + return conf; + } + + public void setConf(ConfId conf) { + this.conf = conf; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((conf == null) ? 0 : conf.hashCode()); + result = prime * result + ((user == null) ? 0 : user.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserConfId other = (UserConfId) obj; + if (conf == null) { + if (other.conf != null) + return false; + } else if (!conf.equals(other.conf)) + return false; + if (user == null) { + if (other.user != null) + return false; + } else if (!user.equals(other.user)) + return false; + return true; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/mapping/UserEntity.java b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserEntity.java new file mode 100644 index 0000000000..135c500744 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/mapping/UserEntity.java @@ -0,0 +1,58 @@ +package org.hibernate.test.mapping; + +import static javax.persistence.CascadeType.ALL; +import static javax.persistence.FetchType.EAGER; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; + +@Entity +@Table(name = "USER") +public class UserEntity implements Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column(name = "user_id") + private Long id; + + @OrderColumn(name = "cnf_order") + @OneToMany(mappedBy="user", fetch = EAGER, cascade = ALL, orphanRemoval = true) + private Set confs = new HashSet(); + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getConfs() { + return confs; + } + + public void setConfs(Set confs) { + this.confs = confs; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}