diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Brand.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Brand.java new file mode 100644 index 0000000000..165ef35824 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Brand.java @@ -0,0 +1,79 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.MapKeyClass; + +import org.hibernate.annotations.MapKeyManyToMany; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Brand { + @Id + @GeneratedValue + private Long id; + + @ManyToMany(targetEntity = LuggageImpl.class) + @MapKeyClass(SizeImpl.class) + private Map luggagesBySize = new HashMap(); + + @ElementCollection(targetClass = SizeImpl.class) + @MapKeyClass(LuggageImpl.class) + @MapKeyManyToMany //legacy column name: was never officially supported BTW + private Map sizePerLuggage = new HashMap(); + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Map getLuggagesBySize() { + return luggagesBySize; + } + + public void setLuggagesBySize(Map luggagesBySize) { + this.luggagesBySize = luggagesBySize; + } + + public Map getSizePerLuggage() { + return sizePerLuggage; + } + + public void setSizePerLuggage(Map sizePerLuggage) { + this.sizePerLuggage = sizePerLuggage; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Luggage.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Luggage.java new file mode 100644 index 0000000000..5714e696af --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Luggage.java @@ -0,0 +1,39 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +/** + * @author Emmanuel Bernard + */ +public interface Luggage { + double getHeight(); + double getWidth(); + + void setHeight(double height); + void setWidth(double width); + + Owner getOwner(); + + void setOwner(Owner owner); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/LuggageImpl.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/LuggageImpl.java new file mode 100644 index 0000000000..48a71c3ba4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/LuggageImpl.java @@ -0,0 +1,78 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.Embedded; + +import org.hibernate.annotations.Target; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class LuggageImpl implements Luggage { + private Long id; + private double height; + private double width; + private Owner owner; + + @Embedded + @Target(OwnerImpl.class) + public Owner getOwner() { + return owner; + } + + public void setOwner(Owner owner) { + this.owner = owner; + } + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Owner.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Owner.java new file mode 100644 index 0000000000..8617ad3433 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Owner.java @@ -0,0 +1,34 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import java.util.Map; + +/** + * @author Emmanuel Bernard + */ +public interface Owner { + String getName(); + void setName(String name); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/OwnerImpl.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/OwnerImpl.java new file mode 100644 index 0000000000..36fbde8c42 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/OwnerImpl.java @@ -0,0 +1,42 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import javax.persistence.Embeddable; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class OwnerImpl implements Owner { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Size.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Size.java new file mode 100644 index 0000000000..1bda657ccc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/Size.java @@ -0,0 +1,32 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +/** + * @author Emmanuel Bernard + */ +public interface Size { + String getName(); + void setName(String name); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/SizeImpl.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/SizeImpl.java new file mode 100644 index 0000000000..885b37b7e9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/SizeImpl.java @@ -0,0 +1,42 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import javax.persistence.Embeddable; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class SizeImpl implements Size { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/target/TargetTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/TargetTest.java new file mode 100644 index 0000000000..7811512145 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/target/TargetTest.java @@ -0,0 +1,104 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.target; + +import org.hibernate.test.annotations.TestCase; +import org.hibernate.Session; + +/** + * @author Emmanuel Bernard + */ +public class TargetTest extends TestCase { + + public void testTargetOnEmbedded() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + Luggage l = new LuggageImpl(); + l.setHeight( 12 ); + l.setWidth( 12 ); + Owner o = new OwnerImpl(); + o.setName( "Emmanuel" ); + l.setOwner( o ); + s.persist( l ); + s.flush(); + s.clear(); + l = (Luggage) s.get(LuggageImpl.class, ( (LuggageImpl) l).getId() ); + assertEquals( "Emmanuel", l.getOwner().getName() ); + s.getTransaction().rollback(); + s.close(); + } + + public void testTargetOnMapKey() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + Luggage l = new LuggageImpl(); + l.setHeight( 12 ); + l.setWidth( 12 ); + Size size = new SizeImpl(); + size.setName( "S" ); + Owner o = new OwnerImpl(); + o.setName( "Emmanuel" ); + l.setOwner( o ); + s.persist( l ); + Brand b = new Brand(); + s.persist( b ); + b.getLuggagesBySize().put( size, l ); + s.flush(); + s.clear(); + b = (Brand) s.get(Brand.class, b.getId() ); + assertEquals( "S", b.getLuggagesBySize().keySet().iterator().next().getName() ); + s.getTransaction().rollback(); + s.close(); + } + + public void testTargetOnMapKeyManyToMany() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + Luggage l = new LuggageImpl(); + l.setHeight( 12 ); + l.setWidth( 12 ); + Size size = new SizeImpl(); + size.setName( "S" ); + Owner o = new OwnerImpl(); + o.setName( "Emmanuel" ); + l.setOwner( o ); + s.persist( l ); + Brand b = new Brand(); + s.persist( b ); + b.getSizePerLuggage().put( l, size ); + s.flush(); + s.clear(); + b = (Brand) s.get(Brand.class, b.getId() ); + assertEquals( 12d, b.getSizePerLuggage().keySet().iterator().next().getWidth() ); + s.getTransaction().rollback(); + s.close(); + } + + protected Class[] getAnnotatedClasses() { + return new Class[] { + LuggageImpl.class, + Brand.class + }; + } +}