diff --git a/documentation/manual/src/main/docbook/en-US/content/type.xml b/documentation/manual/src/main/docbook/en-US/content/type.xml index cc0c42b524af..9714aaad47cf 100644 --- a/documentation/manual/src/main/docbook/en-US/content/type.xml +++ b/documentation/manual/src/main/docbook/en-US/content/type.xml @@ -905,10 +905,17 @@ cfg...; org.hibernate.usertype.CompositeUserType, our Money custom type would look as follows: - + + The org.hibernate.usertype.CompositeUserType may be annotated with a @Columns definition to provide + a default mapping of column names and column properties. This allows the author to specify defaults for the mapping, in order to + reduce the burden of mapping each occurrence of the given type. If a name is specified in the @Column definition of the CustomUserType, + this will be prefixed with the name of the property to give a fully unique name for the mapping. Any defaults specified in this way + may be overridden by adding a mapping on the property where the type is used. + Defining the custom CompositeUserType - returnedClass() { + return Address.class; + } + + public void setPropertyValue(Object component, int propertyIndex, Object value) throws HibernateException { + Address address = (Address) component; + switch (propertyIndex) { + case 0: + address.address1 = (String) value; + break; + case 1: + address.city = (String) value; + default: + break; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/CompositeUserTypePropertyNameTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/CompositeUserTypePropertyNameTest.java new file mode 100644 index 000000000000..eb03846d99f7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/CompositeUserTypePropertyNameTest.java @@ -0,0 +1,69 @@ +package org.hibernate.test.annotations.cut; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import org.hibernate.jdbc.Work; +import org.hibernate.test.annotations.TestCase; +import org.hibernate.tool.hbm2ddl.SchemaExport; + +/** + * @author Frode Carlsen + */ +public class CompositeUserTypePropertyNameTest extends TestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class }; + } + + @Override + protected void configure(Configuration cfg) { + super.configure(cfg); + cfg.registerTypeOverride(AddressCompositeUserType.INSTANCE, new String[] { Address.class.getName() }); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + exportSchema(cfg, getSessions()); + } + + private static void exportSchema(final Configuration cfg, SessionFactory sessFact) { + org.hibernate.classic.Session session = sessFact.openSession(); + session.doWork(new Work() { + public void execute(final Connection conn) throws SQLException { + SchemaExport schemaExport = new SchemaExport(cfg, conn); + schemaExport.create(true, true); + } + }); + session.close(); + } + + public void testBasicOps() { + Session session = openSession(); + session.beginTransaction(); + + Person person = new Person("Steve", new Address()); + person.getAddress().setAddress1("123 Main"); + person.getAddress().setCity("Anywhere"); + + session.persist(person); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.beginTransaction(); + Person person1 = (Person) session.createQuery("from Person p where p.address.addr1 = '123 Main'").uniqueResult(); + assertTrue(person != person1); + session.createQuery("from Person p where p.address.city = 'Anywhere'").list(); + person = (Person) session.load(Person.class, person.getId()); + session.delete(person); + + session.getTransaction().commit(); + session.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/Person.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/Person.java new file mode 100644 index 000000000000..1cbfe616529a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/cut/Person.java @@ -0,0 +1,55 @@ +//$Id: Person.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $ +package org.hibernate.test.annotations.cut; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + Integer id; + + String name; + + Address address; + + @SuppressWarnings("unused") + private Person() { + } + + public Person(String name, Address address) { + this.name = name; + this.address = address; + } + + public Address getAddress() { + return address; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public void setAddress(Address address) { + this.address = address; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + +}