git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18403 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Sharath Reddy 2010-01-04 20:12:33 +00:00
parent ea67575c97
commit d12c535f66
8 changed files with 657 additions and 11 deletions

View File

@ -570,17 +570,19 @@ public class Ejb3JoinColumn extends Ejb3Column {
*/ */
public void overrideFromReferencedColumnIfNecessary(org.hibernate.mapping.Column column) { public void overrideFromReferencedColumnIfNecessary(org.hibernate.mapping.Column column) {
// columnDefinition can also be specified using @JoinColumn, hence we have to check if (getMappingColumn() != null) {
// whether it is set or not // columnDefinition can also be specified using @JoinColumn, hence we have to check
if ( StringHelper.isEmpty( sqlType ) ) { // whether it is set or not
sqlType = column.getSqlType(); if ( StringHelper.isEmpty( sqlType ) ) {
if ( getMappingColumn() != null ) getMappingColumn().setSqlType( sqlType ); sqlType = column.getSqlType();
getMappingColumn().setSqlType( sqlType );
}
// these properties can only be applied on the referenced column - we can just take them over
getMappingColumn().setLength(column.getLength());
getMappingColumn().setPrecision(column.getPrecision());
getMappingColumn().setScale(column.getScale());
} }
// these properties can only be applied on the referenced column - we can just take them over
getMappingColumn().setLength(column.getLength());
getMappingColumn().setPrecision(column.getPrecision());
getMappingColumn().setScale(column.getScale());
} }
@Override @Override

View File

@ -0,0 +1,105 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.JoinColumnOrFormula;
import org.hibernate.annotations.JoinColumnsOrFormulas;
import org.hibernate.annotations.JoinFormula;
@Entity
@Table(name = "CONTRACT")
public class Contract {
private String contractNumber;
// private Integer companyCode;
private ContractId id;
private Manufacturer manufacturer;
private Model model;
public Contract() {
}
@Id
public ContractId getId() {
return id;
}
public void setId(ContractId id) {
this.id = id;
}
@ManyToOne
@JoinColumnsOrFormulas( {
@JoinColumnOrFormula(column = @JoinColumn(name = "CDT_MDL_CODE", referencedColumnName = "MDL_CODE")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "CDT_MFG_CODE", referencedColumnName = "MDL_MFG_CODE")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "CDT_COMPANY_CODE", referencedColumnName = "MDL_COMPANY_CODE"))})
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
@Column(name = "CDT_CONTRACT_NBR")
public String getContractNumber() {
return contractNumber;
}
public void setContractNumber(String contractNumber) {
this.contractNumber = contractNumber;
}
@ManyToOne
@JoinColumnsOrFormulas( {
@JoinColumnOrFormula(column = @JoinColumn(name = "CDT_MFG_CODE", referencedColumnName = "MFG_CODE")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "CDT_COMPANY_CODE", referencedColumnName = "MFG_COMPANY_CODE")) })
public Manufacturer getManufacturer() {
return manufacturer;
}
public void setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
}
@Override
public String toString() {
return contractNumber;
}
}

View File

@ -0,0 +1,124 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ContractId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer companyCode;
private Long contractNumber;
private Integer contractSequenceNumber;
public ContractId() {
}
public ContractId(Integer companyCode, Long contractNumber,
Integer contractSequenceNumber) {
this.companyCode = companyCode;
this.contractNumber = contractNumber;
this.contractSequenceNumber = contractSequenceNumber;
}
@Column(name = "CDT_COMPANY_CODE")
public Integer getCompanyCode() {
return companyCode;
}
public void setCompanyCode(Integer companyCode) {
this.companyCode = companyCode;
}
@Column(name="CDT_NBR")
public Long getContractNumber() {
return contractNumber;
}
public void setContractNumber(Long contractNumber) {
this.contractNumber = contractNumber;
}
@Column(name="CDT_SEQ_NBR")
public Integer getContractSequenceNumber() {
return contractSequenceNumber;
}
public void setContractSequenceNumber(Integer contractSequenceNumber) {
this.contractSequenceNumber = contractSequenceNumber;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyCode == null) ? 0 : companyCode.hashCode());
result = prime * result
+ ((contractNumber == null) ? 0 : contractNumber.hashCode());
result = prime
* result
+ ((contractSequenceNumber == null) ? 0
: contractSequenceNumber.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;
ContractId other = (ContractId) obj;
if (companyCode == null) {
if (other.companyCode != null)
return false;
} else if (!companyCode.equals(other.companyCode))
return false;
if (contractNumber == null) {
if (other.contractNumber != null)
return false;
} else if (!contractNumber.equals(other.contractNumber))
return false;
if (contractSequenceNumber == null) {
if (other.contractSequenceNumber != null)
return false;
} else if (!contractSequenceNumber.equals(other.contractSequenceNumber))
return false;
return true;
}
}

View File

@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "MANUFACTURER")
public class Manufacturer {
private ManufacturerId id;
private String name;
public Manufacturer(ManufacturerId id, String name) {
this.id = id;
this.name = name;
}
public Manufacturer() {
}
@Column(name = "MFG_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
public ManufacturerId getId() {
return id;
}
public void setId(ManufacturerId id) {
this.id = id;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,71 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ManufacturerId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer companyCode;
private Integer manufacturerCode;
public ManufacturerId(Integer companyCode, Integer manufacturerCode) {
this.companyCode = companyCode;
this.manufacturerCode = manufacturerCode;
}
public ManufacturerId() {
}
@Column(name = "MFG_COMPANY_CODE")
public Integer getCompanyCode() {
return companyCode;
}
public void setCompanyCode(Integer companyCode) {
this.companyCode = companyCode;
}
@Column(name = "MFG_CODE")
public Integer getManufacturerCode() {
return manufacturerCode;
}
public void setManufacturerCode(Integer manufacturerCode) {
this.manufacturerCode = manufacturerCode;
}
}

View File

@ -109,6 +109,46 @@ public class ManyToOneWithFormulaTest extends TestCase {
s.close(); s.close();
} }
public void testReferencedColumnNameBelongsToEmbeddedIdOfReferencedEntity() throws Exception {
Session session = openSession();
Transaction tx = session.beginTransaction();
Integer companyCode = 10;
Integer mfgCode = 100;
String contractNumber = "NSAR97841";
ContractId contractId = new ContractId(companyCode, 12457l, 1);
Manufacturer manufacturer = new Manufacturer(new ManufacturerId(
companyCode, mfgCode), "FORD");
Model model = new Model(new ModelId(companyCode, mfgCode, "FOCUS"),
"FORD FOCUS");
session.persist(manufacturer);
session.persist(model);
Contract contract = new Contract();
contract.setId(contractId);
contract.setContractNumber(contractNumber);
contract.setManufacturer(manufacturer);
contract.setModel(model);
session.persist(contract);
session.flush();
session.clear();
contract = (Contract) session.load(Contract.class, contractId);
assertEquals("NSAR97841", contract.getContractNumber());
assertEquals("FORD", contract.getManufacturer().getName());
assertEquals("FORD FOCUS", contract.getModel().getName());
tx.commit();
session.close();
}
/** /**
* @see org.hibernate.test.annotations.TestCase#getMappings() * @see org.hibernate.test.annotations.TestCase#getMappings()
*/ */
@ -119,7 +159,13 @@ public class ManyToOneWithFormulaTest extends TestCase {
Company.class, Company.class,
Person.class, Person.class,
Message.class, Message.class,
Language.class Language.class,
Contract.class,
ContractId.class,
Model.class,
ModelId.class,
Manufacturer.class,
ManufacturerId.class
}; };
} }

View File

@ -0,0 +1,97 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="MODEL")
public class Model {
private ModelId id;
private String name;
public Model(ModelId id, String name) {
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.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;
Model other = (Model) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Model() {
}
@Id
public ModelId getId() {
return id;
}
public void setId(ModelId id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,126 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.annotations.manytoonewithformula;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ModelId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer companyCode;
private Integer manufacturerCode;
private String modelCode;
public ModelId(Integer companyCode, Integer manufacturerCode,
String modelCode) {
this.companyCode = companyCode;
this.manufacturerCode = manufacturerCode;
this.modelCode = modelCode;
}
public ModelId() {
}
@Column(name = "MDL_COMPANY_CODE")
public Integer getCompanyCode() {
return companyCode;
}
public void setCompanyCode(Integer companyCode) {
this.companyCode = companyCode;
}
@Column(name = "MDL_MFG_CODE")
public Integer getManufacturerCode() {
return manufacturerCode;
}
public void setManufacturerCode(Integer manufacturerCode) {
this.manufacturerCode = manufacturerCode;
}
@Column(name = "MDL_CODE")
public String getModelCode() {
return modelCode;
}
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyCode == null) ? 0 : companyCode.hashCode());
result = prime
* result
+ ((manufacturerCode == null) ? 0 : manufacturerCode.hashCode());
result = prime * result
+ ((modelCode == null) ? 0 : modelCode.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;
ModelId other = (ModelId) obj;
if (companyCode == null) {
if (other.companyCode != null)
return false;
} else if (!companyCode.equals(other.companyCode))
return false;
if (manufacturerCode == null) {
if (other.manufacturerCode != null)
return false;
} else if (!manufacturerCode.equals(other.manufacturerCode))
return false;
if (modelCode == null) {
if (other.modelCode != null)
return false;
} else if (!modelCode.equals(other.modelCode))
return false;
return true;
}
}