HHH-9797 - Inaccurate warnings logged about duplicate joins (HHH000072)

This commit is contained in:
Steve Ebersole 2015-05-14 11:52:11 -05:00
parent df898f3fbc
commit 329e85b0b0
8 changed files with 289 additions and 147 deletions

View File

@ -1371,34 +1371,6 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector
return xrefEntry == null ? null : xrefEntry.secondaryTableJoinMap;
}
@Override
public void addJoins(PersistentClass persistentClass, Map<String, Join> joins) {
// this part about resolving the super-EntityTableXref is a best effort. But
// really annotation binding is the only thing calling this, and it does not
// directly use the EntityTableXref stuff, so its all good.
final EntityTableXrefImpl superEntityTableXref;
if ( persistentClass.getSuperclass() == null ) {
superEntityTableXref = null;
}
else {
superEntityTableXref = entityTableXrefMap.get( persistentClass.getSuperclass().getEntityName() );
}
final String primaryTableLogicalName = getLogicalTableName( persistentClass.getTable() );
final EntityTableXrefImpl xrefEntry = new EntityTableXrefImpl(
getDatabase().toIdentifier( primaryTableLogicalName ),
persistentClass.getTable(),
superEntityTableXref
);
final EntityTableXrefImpl old = entityTableXrefMap.put( persistentClass.getEntityName(), xrefEntry );
if ( old != null ) {
log.duplicateJoins( persistentClass.getEntityName() );
}
xrefEntry.secondaryTableJoinMap = joins;
}
private final class EntityTableXrefImpl implements EntityTableXref {
private final Identifier primaryTableLogicalName;
private final Table primaryTable;

View File

@ -292,10 +292,10 @@ public interface InFlightMetadataCollector extends Mapping, MetadataImplementor
NaturalIdUniqueKeyBinder locateNaturalIdUniqueKeyBinder(String entityName);
void registerNaturalIdUniqueKeyBinder(String entityName, NaturalIdUniqueKeyBinder ukBinder);
public static interface DelayedPropertyReferenceHandler extends Serializable {
public void process(InFlightMetadataCollector metadataCollector);
interface DelayedPropertyReferenceHandler extends Serializable {
void process(InFlightMetadataCollector metadataCollector);
}
public void addDelayedPropertyReferenceHandler(DelayedPropertyReferenceHandler handler);
void addDelayedPropertyReferenceHandler(DelayedPropertyReferenceHandler handler);
void addPropertyReference(String entityName, String propertyName);
void addUniquePropertyReference(String entityName, String propertyName);
@ -310,7 +310,7 @@ public interface InFlightMetadataCollector extends Mapping, MetadataImplementor
void addJpaIndexHolders(Table table, List<JPAIndexHolder> jpaIndexHolders);
public static interface EntityTableXref {
interface EntityTableXref {
void addSecondaryTable(LocalMetadataBuildingContext buildingContext, Identifier logicalName, Join secondaryTableJoin);
void addSecondaryTable(Identifier logicalName, Join secondaryTableJoin);
Table resolveTable(Identifier tableName);
@ -318,7 +318,7 @@ public interface InFlightMetadataCollector extends Mapping, MetadataImplementor
Join locateJoin(Identifier tableName);
}
public static class DuplicateSecondaryTableException extends HibernateException {
class DuplicateSecondaryTableException extends HibernateException {
private final Identifier tableName;
public DuplicateSecondaryTableException(Identifier tableName) {
@ -333,8 +333,11 @@ public interface InFlightMetadataCollector extends Mapping, MetadataImplementor
}
}
public EntityTableXref getEntityTableXref(String entityName);
public EntityTableXref addEntityTableXref(String entityName, Identifier primaryTableLogicalName, Table primaryTable, EntityTableXref superEntityTableXref);
void addJoins(PersistentClass persistentClass, Map<String, Join> secondaryTables);
EntityTableXref getEntityTableXref(String entityName);
EntityTableXref addEntityTableXref(
String entityName,
Identifier primaryTableLogicalName,
Table primaryTable,
EntityTableXref superEntityTableXref);
Map<String,Join> getJoins(String entityName);
}

View File

@ -136,6 +136,8 @@ public class EntityBinder {
private boolean lazy;
private XClass proxyClass;
private String where;
// todo : we should defer to InFlightMetadataCollector.EntityTableXref for secondary table tracking;
// atm we use both from here; HBM binding solely uses InFlightMetadataCollector.EntityTableXref
private java.util.Map<String, Join> secondaryTables = new HashMap<String, Join>();
private java.util.Map<String, Object> secondaryTableJoins = new HashMap<String, Object>();
private String cacheConcurrentStrategy;
@ -698,7 +700,6 @@ public class EntityBinder {
Join join = (Join) joins.next();
createPrimaryColumnsToSecondaryTable( uncastedColumn, propertyHolder, join );
}
context.getMetadataCollector().addJoins( persistentClass, secondaryTables );
}
private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {

View File

@ -1,35 +1,61 @@
//$Id: Customer.java 4364 2004-08-17 12:10:32Z oneovthafew $
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SecondaryTable;
/**
* @author Gavin King
* @author Steve Ebersole
*/
@Entity
@DiscriminatorValue( "C" )
@SecondaryTable( name="customer" )
public class Customer extends Person {
private Employee salesperson;
private String comments;
/**
* @return Returns the salesperson.
*/
@ManyToOne
@JoinColumn( table = "customer" )
public Employee getSalesperson() {
return salesperson;
}
/**
* @param salesperson The salesperson to set.
*/
public void setSalesperson(Employee salesperson) {
this.salesperson = salesperson;
}
/**
* @return Returns the comments.
*/
@Column( table = "customer" )
public String getComments() {
return comments;
}
/**
* @param comments The comments to set.
*/
public void setComments(String comments) {
this.comments = comments;
}

View File

@ -1,47 +1,72 @@
//$Id: Employee.java 4364 2004-08-17 12:10:32Z oneovthafew $
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.join;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SecondaryTable;
/**
* @author Gavin King
* @author Steve Ebersole
*/
@Entity
@DiscriminatorValue( "E" )
@SecondaryTable( name = "employee" )
public class Employee extends Person {
private String title;
private BigDecimal salary;
private Employee manager;
/**
* @return Returns the title.
*/
@Column( table = "employee", name = "`title`")
public String getTitle() {
return title;
}
/**
* @param title The title to set.
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return Returns the manager.
*/
@ManyToOne
@JoinColumn( table = "employee" )
public Employee getManager() {
return manager;
}
/**
* @param manager The manager to set.
*/
public void setManager(Employee manager) {
this.manager = manager;
}
/**
* @return Returns the salary.
*/
@Column( table = "employee" )
public BigDecimal getSalary() {
return salary;
}
/**
* @param salary The salary to set.
*/
public void setSalary(BigDecimal salary) {
this.salary = salary;
}

View File

@ -1,11 +1,54 @@
//$Id: Person.java 7203 2005-06-19 02:01:05Z oneovthafew $
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Transient;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.GenericGenerator;
/**
* @author Gavin King
* @author Steve Ebersole
*/
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name = "person_type" )
@DiscriminatorValue( value = "P" )
@SecondaryTable( name = "address", pkJoinColumns = @PrimaryKeyJoinColumn(name = "address_id") )
public class Person {
private long id;
private String name;
@ -15,90 +58,72 @@ public class Person {
private double heightInches;
private char sex;
/**
* @return Returns the sex.
*/
public char getSex() {
return sex;
}
/**
* @param sex The sex to set.
*/
public void setSex(char sex) {
this.sex = sex;
}
/**
* @return Returns the id.
*/
@Id
@GeneratedValue( generator = "increment" )
@GenericGenerator( name = "increment", strategy = "increment" )
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the identity.
*/
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getName() {
return name;
}
/**
* @param identity The identity to set.
*/
public void setName(String identity) {
this.name = identity;
}
@Transient
public String getSpecies() {
return null;
}
/**
* @return Returns the country.
*/
public String getCountry() {
return country;
}
/**
* @param country The country to set.
*/
public void setCountry(String country) {
this.country = country;
}
/**
* @return Returns the zip.
*/
public String getZip() {
return zip;
}
/**
* @param zip The zip to set.
*/
public void setZip(String zip) {
this.zip = zip;
}
/**
* @return the The height in inches.
*/
@Column( name = "height_centimeters" )
@ColumnTransformer( read = "height_centimeters / 2.54E0", write = "? * 2.54E0" )
public double getHeightInches() {
return heightInches;
}
/**
* @param heightInches The height in inches.
*/
public void setHeightInches(double heightInches) {
this.heightInches = heightInches;
}
/**
* @param address The address to set.
*/
@Column(table = "address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
@Column(table = "address")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(table = "address")
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}

View File

@ -0,0 +1,51 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.join;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Copy of the model used in JoinTest, but using annotations rather than hbm.xml to look
* for the duplicate joins warning
*
* @author Steve Ebersole
*/
public class SecondaryTableTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return null;
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Person.class, Employee.class, Customer.class, User.class};
}
@Test
public void testIt() {
// really we have nothing to tes
}
}

View File

@ -1,37 +1,76 @@
//$Id$
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.join;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import org.hibernate.annotations.ColumnTransformer;
/**
* @author Mike Dillon
* @author Steve Ebersole
*/
@Entity
@DiscriminatorValue( "U" )
@SecondaryTables({
@SecondaryTable(name = "t_user"),
@SecondaryTable(name = "t_silly")
})
public class User extends Person {
private String login;
private String silly;
private Double passwordExpiryDays;
private String silly;
/**
* @return Returns the login.
*/
@Column(table = "t_user", name = "u_login")
public String getLogin() {
return login;
}
/**
* @param login The login to set.
*/
public void setLogin(String login) {
this.login = login;
}
/**
* @return The password expiry policy in days.
*/
@Column(table = "t_user", name = "pwd_expiry_weeks")
@ColumnTransformer( read = "pwd_expiry_weeks * 7.0E0", write = "? / 7.0E0")
public Double getPasswordExpiryDays() {
return passwordExpiryDays;
}
/**
* @param passwordExpiryDays The password expiry policy in days.
*/
public void setPasswordExpiryDays(Double passwordExpiryDays) {
this.passwordExpiryDays = passwordExpiryDays;
}
@Column(table = "t_silly")
public String getSilly() {
return silly;
}
public void setSilly(String silly) {
this.silly = silly;
}
}