Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-7310 Resolution of types registered in type registry does not wor…
…k properly for Properties of @embeddable types
- Loading branch information
Showing
10 changed files
with
633 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/DollarValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, 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.annotations.embeddables; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
|
||
/** | ||
* @author Chris Pheby | ||
*/ | ||
public class DollarValue implements Serializable { | ||
|
||
private static final long serialVersionUID = -416056386419355705L; | ||
|
||
private BigDecimal amount; | ||
|
||
public DollarValue() {}; | ||
|
||
public DollarValue(BigDecimal amount) { | ||
this.amount = amount; | ||
} | ||
|
||
public BigDecimal getAmount() { | ||
return amount; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...te-core/src/test/java/org/hibernate/test/annotations/embeddables/DollarValueUserType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, 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.annotations.embeddables; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
|
||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SessionImplementor; | ||
import org.hibernate.usertype.UserType; | ||
|
||
/** | ||
* @author Chris Pheby | ||
*/ | ||
public class DollarValueUserType implements UserType { | ||
|
||
@Override | ||
public int[] sqlTypes() { | ||
return new int[] {Types.BIGINT}; | ||
} | ||
|
||
@Override | ||
public Class<DollarValue> returnedClass() { | ||
return DollarValue.class; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object x, Object y) throws HibernateException { | ||
if (!(x instanceof DollarValue) || !(y instanceof DollarValue)) { | ||
throw new HibernateException("Expected DollarValue"); | ||
} | ||
return ((DollarValue)x).getAmount().equals(((DollarValue)y).getAmount()); | ||
} | ||
|
||
@Override | ||
public int hashCode(Object x) throws HibernateException { | ||
if (!(x instanceof DollarValue)) { | ||
throw new HibernateException("Expected DollarValue"); | ||
} | ||
return ((DollarValue)x).getAmount().hashCode(); | ||
} | ||
|
||
@Override | ||
public DollarValue nullSafeGet(ResultSet rs, String[] names, | ||
SessionImplementor session, Object owner) | ||
throws HibernateException, SQLException { | ||
|
||
DollarValue result = new DollarValue(rs.getBigDecimal(rs.findColumn(names[0]))); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void nullSafeSet(PreparedStatement st, Object value, int index, | ||
SessionImplementor session) throws HibernateException, SQLException { | ||
|
||
st.setBigDecimal(index, ((DollarValue)value).getAmount()); | ||
} | ||
|
||
@Override | ||
public Object deepCopy(Object value) throws HibernateException { | ||
DollarValue result = new DollarValue(); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(Object value) throws HibernateException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object assemble(Serializable cached, Object owner) | ||
throws HibernateException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object replace(Object original, Object target, Object owner) | ||
throws HibernateException { | ||
return null; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...re/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, 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.annotations.embeddables; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.exception.GenericJDBCException; | ||
import org.hibernate.integrator.internal.IntegratorServiceImpl; | ||
import org.hibernate.integrator.spi.Integrator; | ||
import org.hibernate.integrator.spi.IntegratorService; | ||
import org.hibernate.service.ServiceRegistry; | ||
import org.hibernate.service.ServiceRegistryBuilder; | ||
import org.hibernate.service.classloading.internal.ClassLoaderServiceImpl; | ||
import org.hibernate.service.classloading.spi.ClassLoaderService; | ||
import org.hibernate.service.internal.BootstrapServiceRegistryImpl; | ||
import org.hibernate.testing.junit4.BaseUnitTestCase; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Chris Pheby | ||
*/ | ||
public class EmbeddableIntegratorTest extends BaseUnitTestCase { | ||
|
||
/** | ||
* Throws a mapping exception because DollarValue is not mapped | ||
*/ | ||
@Test(expected=GenericJDBCException.class) | ||
public void testWithoutIntegrator() { | ||
|
||
ServiceRegistry reg = new ServiceRegistryBuilder(new BootstrapServiceRegistryImpl()) | ||
.buildServiceRegistry(); | ||
|
||
SessionFactory sf = new Configuration() | ||
.addAnnotatedClass( Investor.class ) | ||
|
||
.buildSessionFactory(reg); | ||
|
||
Session sess = sf.openSession(); | ||
Investor myInv = getInvestor(); | ||
myInv.setId(1L); | ||
|
||
sess.save(myInv); | ||
sess.flush(); | ||
sess.clear(); | ||
|
||
Investor inv = (Investor) sess.get(Investor.class, 1L); | ||
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount()); | ||
|
||
sess.close(); | ||
} | ||
|
||
@Test | ||
public void testWithIntegrator() { | ||
|
||
LinkedHashSet<Integrator> providedIntegrators = new LinkedHashSet<Integrator>(); | ||
providedIntegrators.add(new InvestorIntegrator()); | ||
ClassLoaderService classLoaderService = new ClassLoaderServiceImpl(); | ||
IntegratorService integratorService = new IntegratorServiceImpl(providedIntegrators, classLoaderService); | ||
|
||
ServiceRegistry reg = new ServiceRegistryBuilder(new BootstrapServiceRegistryImpl( | ||
classLoaderService, | ||
integratorService)).buildServiceRegistry(); | ||
|
||
SessionFactory sf = new Configuration() | ||
.addAnnotatedClass( Investor.class ) | ||
|
||
.setProperty("hibernate.hbm2ddl.auto", "create-drop") | ||
.buildSessionFactory(reg); | ||
|
||
Session sess = sf.openSession(); | ||
Investor myInv = getInvestor(); | ||
myInv.setId(2L); | ||
|
||
sess.save(myInv); | ||
sess.flush(); | ||
sess.clear(); | ||
|
||
Investor inv = (Investor) sess.get(Investor.class, 2L); | ||
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount()); | ||
|
||
sess.close(); | ||
} | ||
|
||
private Investor getInvestor() { | ||
Investor i = new Investor(); | ||
List<Investment> investments = new ArrayList<Investment>(); | ||
Investment i1 = new Investment(); | ||
i1.setAmount(new DollarValue(new BigDecimal("100"))); | ||
i1.setDate(new MyDate(new Date())); | ||
i1.setDescription("Test Investment"); | ||
investments.add(i1); | ||
i.setInvestments(investments); | ||
|
||
return i; | ||
} | ||
} |
Oops, something went wrong.