if (id == null) {
throw new NullPointerException("The id is null");
}
BeanDescriptor desc = getBeanDescriptor(type);
// convert the id type if necessary
id = desc.convertId(id);
Object ref = null;
PersistenceContext ctx = null;
SpiTransaction t = transactionScopeManager.get();
if (t != null) {
// first try the persistence context
ctx = t.getPersistenceContext();
ref = ctx.get(type, id);
}
if (ref == null) {
InheritInfo inheritInfo = desc.getInheritInfo();
if (inheritInfo != null) {
// we actually need to do a query because
// we don't know the type without the
// discriminator value
BeanProperty idProp = desc.getIdProperty();
if (idProp == null) {
throw new PersistenceException("No ID properties for this type? " + desc);
}
// just select the id properties and
// the discriminator column (auto added)
Query<T> query = createQuery(type);
query.select(idProp.getName()).setId(id);
ref = query.findUnique();
} else {
// use the default reference options
ref = desc.createReference(null, id);
}
if (ctx != null && (ref instanceof EntityBean)) {
// Not putting a vanilla reference in the persistence context
ctx.put(id, ref);