OK, I've managed to narrow this down to only affecting ComponentTypes.
Here's an example:
Code:
package com.jc.test;
import javax.persistence.Embeddable;
@Embeddable
public class ExternalSystemOrderIdentifier {
private String sourceSystem;
private Long externalId;
public ExternalSystemOrderIdentifier() {
// hibernate
}
public ExternalSystemOrderIdentifier(String sourceSystem, Long externalId) {
this.sourceSystem = sourceSystem;
this.externalId = externalId;
}
public String getSourceSystem() {
return sourceSystem;
}
public Long getExternalId() {
return externalId;
}
}
Code:
package com.jc.test;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Embedded
private ExternalSystemOrderIdentifier externalIdentifier;
private String symbol;
private int quantity;
private double price;
public Order() {
// hibernate
}
public Order(ExternalSystemOrderIdentifier externalIdentifier, String symbol, int quantity, double price) {
this.externalIdentifier = externalIdentifier;
this.symbol = symbol;
this.quantity = quantity;
this.price = price;
}
public ExternalSystemOrderIdentifier getExternalIdentifier() {
return externalIdentifier;
}
public String getSymbol() {
return symbol;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public Long getId(){
return id;
}
}
Code:
package com.jc.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;
public class OrderBook {
private final SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
@SuppressWarnings("unchecked")
public List<Order> find(ExternalSystemOrderIdentifier identifier) {
Session session = sessionFactory.openSession();
try {
return session.createCriteria(Order.class).add(Restrictions.eq("externalIdentifier", identifier)).setCacheable(true).list();
} finally {
session.close();
}
}
public void add(Order order) {
Session session = sessionFactory.openSession();
Transaction trans = session.beginTransaction();
try {
trans.begin();
session.save(order);
trans.commit();
} finally {
session.close();
}
}
}
Code:
package com.jc.test;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestRunner {
@Test
public void testDodginess() {
OrderBook orderBook = new OrderBook();
Order order = new Order(new ExternalSystemOrderIdentifier("SomeSystem", 1l), "DE0009542977", 3000, 15.2);
orderBook.add(order);
assertEquals(1, orderBook.find(order.getExternalIdentifier()).size());
}
}
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">...</property>
<property name="connection.username">...</property>
<property name="connection.password">...</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.dbcp.minIdle">10</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.initialSize">10</property>
<property name="hibernate.dbcp.maxActive">10</property>
<property name="hibernate.jdbc.batch_size">250</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.dbcp.poolPreparedStatements">true</property>
<property name="hibernate.dbcp.maxOpenPreparedStatements">10</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping class="com.jc.test.Order" />
</session-factory>
</hibernate-configuration>