diff --git a/annotations/src/main/java/org/hibernate/annotations/JoinColumnOrFormula.java b/annotations/src/main/java/org/hibernate/annotations/JoinColumnOrFormula.java new file mode 100644 index 0000000000..53fb0f4463 --- /dev/null +++ b/annotations/src/main/java/org/hibernate/annotations/JoinColumnOrFormula.java @@ -0,0 +1,48 @@ +/* + * 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.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.persistence.JoinColumn; + +/** + * @author Sharath Reddy + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) +public @interface JoinColumnOrFormula { + JoinFormula formula() default @JoinFormula(value="", referencedColumnName=""); + JoinColumn column() default @JoinColumn(); +} diff --git a/annotations/src/main/java/org/hibernate/annotations/JoinColumnsOrFormulas.java b/annotations/src/main/java/org/hibernate/annotations/JoinColumnsOrFormulas.java new file mode 100644 index 0000000000..e8aed4e6bd --- /dev/null +++ b/annotations/src/main/java/org/hibernate/annotations/JoinColumnsOrFormulas.java @@ -0,0 +1,43 @@ +/* + * 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.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * @author Sharath Reddy + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) +public @interface JoinColumnsOrFormulas { + JoinColumnOrFormula [] value(); +} diff --git a/annotations/src/main/java/org/hibernate/annotations/JoinFormula.java b/annotations/src/main/java/org/hibernate/annotations/JoinFormula.java new file mode 100644 index 0000000000..0ba3a68e74 --- /dev/null +++ b/annotations/src/main/java/org/hibernate/annotations/JoinFormula.java @@ -0,0 +1,47 @@ +/* + * 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.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** + * JoinFormula. To be used as a replacement for @JoinColumn in most places + * The formula has to be a valid SQL fragment + * + * @author Sharath Reddy + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) +public @interface JoinFormula { + String value(); + String referencedColumnName() default ""; +} diff --git a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 18ed9b3b07..cd90f0e00e 100644 --- a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -101,6 +101,7 @@ import org.hibernate.annotations.ForeignKey; import org.hibernate.annotations.Formula; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Index; +import org.hibernate.annotations.JoinColumnsOrFormulas; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.annotations.ManyToAny; @@ -1219,6 +1220,13 @@ public final class AnnotationBinder { propertyHolder, inferredData.getPropertyName(), mappings ); } + else if ( property.isAnnotationPresent( JoinColumnsOrFormulas.class ) ) { + JoinColumnsOrFormulas ann = property.getAnnotation( JoinColumnsOrFormulas.class ); + joinColumns = Ejb3JoinColumn.buildJoinColumnsOrFormulas( + ann, null, entityBinder.getSecondaryTables(), + propertyHolder, inferredData.getPropertyName(), mappings + ); + } } if ( property.isAnnotationPresent( Column.class ) || property.isAnnotationPresent( Formula.class ) ) { Column ann = property.getAnnotation( Column.class ); diff --git a/annotations/src/main/java/org/hibernate/cfg/Ejb3Column.java b/annotations/src/main/java/org/hibernate/cfg/Ejb3Column.java index c110071516..328a4be292 100644 --- a/annotations/src/main/java/org/hibernate/cfg/Ejb3Column.java +++ b/annotations/src/main/java/org/hibernate/cfg/Ejb3Column.java @@ -95,6 +95,10 @@ public class Ejb3Column { return unique; } + public boolean isFormula() { + return StringHelper.isNotEmpty(formulaString) ? true : false; + } + public String getFormulaString() { return formulaString; } @@ -188,16 +192,22 @@ public class Ejb3Column { String columnName, String propertyName, int length, int precision, int scale, boolean nullable, String sqlType, boolean unique, boolean applyNamingStrategy ) { - this.mappingColumn = new Column(); - redefineColumnName( columnName, propertyName, applyNamingStrategy ); - this.mappingColumn.setLength( length ); - if ( precision > 0 ) { //revelent precision - this.mappingColumn.setPrecision( precision ); - this.mappingColumn.setScale( scale ); + if ( StringHelper.isNotEmpty( formulaString ) ) { + this.formula = new Formula(); + this.formula.setFormula( formulaString ); + } + else { + this.mappingColumn = new Column(); + redefineColumnName( columnName, propertyName, applyNamingStrategy ); + this.mappingColumn.setLength( length ); + if ( precision > 0 ) { //revelent precision + this.mappingColumn.setPrecision( precision ); + this.mappingColumn.setScale( scale ); + } + this.mappingColumn.setNullable( nullable ); + this.mappingColumn.setSqlType( sqlType ); + this.mappingColumn.setUnique( unique ); } - this.mappingColumn.setNullable( nullable ); - this.mappingColumn.setSqlType( sqlType ); - this.mappingColumn.setUnique( unique ); } public boolean isNameDeferred() { @@ -462,8 +472,14 @@ public class Ejb3Column { public static void checkPropertyConsistency(Ejb3Column[] columns, String propertyName) { int nbrOfColumns = columns.length; + if ( nbrOfColumns > 1 ) { for (int currentIndex = 1; currentIndex < nbrOfColumns; currentIndex++) { + + if (columns[currentIndex].isFormula() || columns[currentIndex - 1].isFormula()) { + continue; + } + if ( columns[currentIndex].isInsertable() != columns[currentIndex - 1].isInsertable() ) { throw new AnnotationException( "Mixing insertable and non insertable columns in a property is not allowed: " + propertyName @@ -486,6 +502,7 @@ public class Ejb3Column { } } } + } public void addIndex(Index index, boolean inSecondPass) { @@ -517,4 +534,4 @@ public class Ejb3Column { ); } } -} \ No newline at end of file +} diff --git a/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java index 0232f77ec9..b2be751983 100644 --- a/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java +++ b/annotations/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java @@ -33,6 +33,9 @@ import javax.persistence.PrimaryKeyJoinColumn; import org.hibernate.AnnotationException; import org.hibernate.MappingException; import org.hibernate.util.StringHelper; +import org.hibernate.annotations.JoinColumnOrFormula; +import org.hibernate.annotations.JoinColumnsOrFormulas; +import org.hibernate.annotations.JoinFormula; import org.hibernate.mapping.Column; import org.hibernate.mapping.Join; import org.hibernate.mapping.PersistentClass; @@ -127,6 +130,55 @@ public class Ejb3JoinColumn extends Ejb3Column { return referencedColumn; } + + public static Ejb3JoinColumn[] buildJoinColumnsOrFormulas( + JoinColumnsOrFormulas anns, + String mappedBy, Map joins, + PropertyHolder propertyHolder, + String propertyName, + ExtendedMappings mappings + ) { + + JoinColumnOrFormula [] ann = anns.value(); + Ejb3JoinColumn [] joinColumns = new Ejb3JoinColumn[ann.length]; + for (int i = 0; i < ann.length; i++) { + JoinColumnOrFormula join = (JoinColumnOrFormula) ann[i]; + JoinFormula formula = join.formula(); + if (formula.value() != null && !formula.value().equals("")) { + joinColumns[i] = buildJoinFormula(formula, mappedBy, joins, propertyHolder, propertyName, mappings); + } + else { + joinColumns[i] = buildJoinColumns(new JoinColumn[] { join.column() }, mappedBy, joins, propertyHolder, propertyName, mappings)[0]; + } + } + + return joinColumns; + } + + /** + * build join formula + */ + private static Ejb3JoinColumn buildJoinFormula( + JoinFormula ann, + String mappedBy, Map joins, + PropertyHolder propertyHolder, + String propertyName, + ExtendedMappings mappings + ) { + + Ejb3JoinColumn formulaColumn = new Ejb3JoinColumn(); + formulaColumn.setFormula( ann.value() ); + formulaColumn.setReferencedColumn(ann.referencedColumnName()); + formulaColumn.setMappings( mappings ); + formulaColumn.setPropertyHolder( propertyHolder ); + formulaColumn.setJoins( joins ); + formulaColumn.setPropertyName( BinderHelper.getRelativePath( propertyHolder, propertyName ) ); + + formulaColumn.bind(); + return formulaColumn; + } + + public static Ejb3JoinColumn[] buildJoinColumns( JoinColumn[] anns, String mappedBy, Map joins, @@ -382,9 +434,10 @@ public class Ejb3JoinColumn extends Ejb3Column { null, referencedColumn.getLength(), referencedColumn.getPrecision(), referencedColumn.getScale(), - getMappingColumn().isNullable(), + getMappingColumn() != null ? getMappingColumn().isNullable() : false, referencedColumn.getSqlType(), - getMappingColumn().isUnique(), false + getMappingColumn() != null ? getMappingColumn().isUnique() : false, + false ); linkWithValue( value ); } @@ -582,4 +635,4 @@ public class Ejb3JoinColumn extends Ejb3Column { this.mappedByTableName = logicalTableName; this.mappedByPropertyName = mappedByProperty; } -} \ No newline at end of file +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Company.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Company.java new file mode 100644 index 0000000000..b54f30b339 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Company.java @@ -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.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import org.hibernate.annotations.JoinColumnOrFormula; +import org.hibernate.annotations.JoinColumnsOrFormulas; +import org.hibernate.annotations.JoinFormula; + +/** + * @author Sharath Reddy + */ +@Entity +public class Company implements Serializable { + + private static final long serialVersionUID = 1L; + private int id; + private Person person; + + @Id @GeneratedValue + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @ManyToOne + @JoinColumnsOrFormulas( + { + @JoinColumnOrFormula(column=@JoinColumn(name="id", referencedColumnName="company_id", updatable=false, insertable=false)), + @JoinColumnOrFormula(formula=@JoinFormula(value="'T'", referencedColumnName="is_default")) + }) + public Person getDefaultContactPerson() { + return person; + } + public void setDefaultContactPerson(Person person) { + this.person = person; + } + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/FoodItem.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/FoodItem.java new file mode 100644 index 0000000000..4b19402218 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/FoodItem.java @@ -0,0 +1,79 @@ +/* + * 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.GeneratedValue; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +import org.hibernate.annotations.JoinColumnOrFormula; +import org.hibernate.annotations.JoinColumnsOrFormulas; +import org.hibernate.annotations.JoinFormula; + +/** + * @author Sharath Reddy + */ +@Entity +public class FoodItem { + private Integer id; + private String item; + private Menu order; + + @Id @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + @ManyToOne + @JoinColumnsOrFormulas( + { + @JoinColumnOrFormula(column=@JoinColumn(name="order_nbr", referencedColumnName="order_nbr")), + @JoinColumnOrFormula(formula=@JoinFormula(value="'F'", referencedColumnName="is_default")) + }) + public Menu getOrder() { + return order; + } + + public void setOrder(Menu order) { + this.order = order; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Language.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Language.java new file mode 100644 index 0000000000..3a85ac3993 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Language.java @@ -0,0 +1,64 @@ +/* + * 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.Entity; +import javax.persistence.Id; + +/** + * @author Sharath Reddy + */ +@Entity +public class Language implements Serializable { + + private static final long serialVersionUID = 1L; + + private String name; + private String code; + + @Id + @Column(name="code") + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } + + @Column(name="name") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java new file mode 100644 index 0000000000..0fbd33bc16 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java @@ -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 org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.test.annotations.TestCase; + +/** + * @author Sharath Reddy + */ +public class ManyToOneWithFormulaTest extends TestCase { + + public ManyToOneWithFormulaTest(String x) { + super( x ); + } + + public void testManyToOneFromNonPk() throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + Menu menu = new Menu(); + menu.setOrderNbr( "123" ); + menu.setDefault("F"); + s.persist( menu ); + FoodItem foodItem = new FoodItem(); + foodItem.setItem( "Mouse" ); + foodItem.setOrder( menu ); + s.persist( foodItem ); + s.flush(); + s.clear(); + foodItem = (FoodItem) s.get( FoodItem.class, foodItem.getId() ); + assertNotNull( foodItem.getOrder() ); + assertEquals( "123", foodItem.getOrder().getOrderNbr() ); + tx.rollback(); + s.close(); + } + + + public void testManyToOneFromPk() throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Company company = new Company(); + s.persist( company ); + + Person person = new Person(); + person.setDefaultFlag("T"); + person.setCompanyId(company.getId()); + s.persist(person); + + s.flush(); + s.clear(); + + company = (Company) s.get( Company.class, company.getId() ); + assertNotNull( company.getDefaultContactPerson() ); + assertEquals( person.getId(), company.getDefaultContactPerson().getId() ); + tx.rollback(); + s.close(); + } + + public void testManyToOneToPkWithOnlyFormula() throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Language language = new Language(); + language.setCode("EN"); + language.setName("English"); + s.persist( language ); + + Message msg = new Message(); + msg.setLanguageCode("en"); + msg.setLanguageName("English"); + s.persist(msg); + + s.flush(); + s.clear(); + + msg = (Message) s.get( Message.class, msg.getId() ); + assertNotNull( msg.getLanguage()); + assertEquals( "EN", msg.getLanguage().getCode() ); + tx.rollback(); + s.close(); + } + + /** + * @see org.hibernate.test.annotations.TestCase#getMappings() + */ + protected java.lang.Class[] getMappings() { + return new java.lang.Class[]{ + Menu.class, + FoodItem.class, + Company.class, + Person.class, + Message.class, + Language.class + }; + } + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Menu.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Menu.java new file mode 100644 index 0000000000..bccdecd86b --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Menu.java @@ -0,0 +1,76 @@ +/* + * 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.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Column; + +/** + * @author Sharath Reddy + */ +@Entity +public class Menu implements Serializable { + + private static final long serialVersionUID = 1L; + private Integer id; + private String orderNbr; + private String isDefault; + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @Column(name="is_default") + public String isDefault() { + return isDefault; + } + + public void setDefault(String isDefault) { + this.isDefault = isDefault; + } + + @Column(name="order_nbr") + public String getOrderNbr() { + return orderNbr; + } + + public void setOrderNbr(String orderNbr) { + this.orderNbr = orderNbr; + } + + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Message.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Message.java new file mode 100644 index 0000000000..7a2b18dc7b --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Message.java @@ -0,0 +1,94 @@ +/* + * 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.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import org.hibernate.annotations.JoinColumnOrFormula; +import org.hibernate.annotations.JoinColumnsOrFormulas; +import org.hibernate.annotations.JoinFormula; + +/** + * @author Sharath Reddy + */ +@Entity +public class Message implements Serializable { + + private static final long serialVersionUID = 1L; + private int id; + private String languageCode; + private String languageName; + private Language language; + + @Id @GeneratedValue + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Column(name="lang_code") + public String getLanguageCode() { + return languageCode; + } + + public void setLanguageCode(String val) { + this.languageCode = val; + } + + @Column(name="lang_name") + public String getLanguageName() { + return languageName; + } + + public void setLanguageName(String val) { + this.languageName = val; + } + + @ManyToOne + @JoinColumnsOrFormulas( + { + @JoinColumnOrFormula(formula=@JoinFormula(value="UPPER(lang_code)")) + //@JoinColumnOrFormula(formula=@JoinFormula(value="(select l.code from Language l where l.name = lang_name)")) + }) + public Language getLanguage() { + return language; + } + public void setLanguage(Language language) { + this.language = language; + } + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Person.java b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Person.java new file mode 100644 index 0000000000..9dd2a87ea2 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/manytoonewithformula/Person.java @@ -0,0 +1,68 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC. + * + * 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.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Sharath Reddy + */ +@Entity +public class Person implements Serializable { + + private static final long serialVersionUID = 1L; + private int id; + private int companyId; + private String defaultFlag; + + @Id @GeneratedValue + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column(name="company_id") + public int getCompanyId() { + return companyId; + } + public void setCompanyId(int companyId) { + this.companyId = companyId; + } + @Column(name="is_default") + public String isDefaultFlag() { + return defaultFlag; + } + public void setDefaultFlag(String defaultFlag) { + this.defaultFlag = defaultFlag; + } +} +