我目前正在做一个从 Apache OJB 到 Hibernate 的端口。我正在使用的应用程序要求我在运行时构建 Hibernate 类映射。为了实现这一点,我使用了我们专有的元数据,并将每个模型类的字段映射到新的 Hibernate 类,就像我们在 Apache OJB 中成功所做的那样。下面我将包括相关的类。映射类后,我从休眠中得到一个检查列重复错误:它是一个空指针,这让我相信这些列没有很好地映射。启动对 buildSessionFactory 的调用时会引发错误。谢谢你的任何想法!
The Error:
java.lang.NullPointerException at
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:723)
The Classes:
public class HibernateMetadataConverter implements MetadataConverter
{
public static Configuration cfg =
HibernateConfigurator.getInstance()
.setProperty("default_entity_mode", "dynamic- map");
public int count = 0;
Mappings mappings = cfg.createMappings();
public void convert(Table table, Map<Class<?>, Table> tables)
{
// First setup the table
org.hibernate.mapping.Table hibTable =
mappings.addTable("montecarlo", null, table.getObjectType().getName(),
null, false);
hibTable.setName(table.getObjectType().getName());
hibTable.setSchema("montecarlo");
mappings.addTableBinding(hibTable.getSchema(), null,
hibTable.getName(), hibTable.getName(), null);
Property property;
SimpleValue value;
// Create PersistentClass with rootClass, sub of PersistentClass
RootClass clazz = new RootClass();
clazz.setEntityName(hibTable.getName());
clazz.setJpaEntityName(table.getObjectType().getName());
clazz.setLazy(true);
clazz.setTable(hibTable);
clazz.setNodeName(hibTable.getName());
//Iterate metadata columns and create hibernate objects
for (Column f : table.getColumns())
{
// System.out.println("ojb table name is " + table.getName() + "\n");
/* String tableFields = table.toString();
if (tableFields.contains("stored")) {
System.out.println("skipping table stored in other table");
}
else
{
*/
if (table.getObjectType().getName().equals("gov.lanl.ldrd.montecarlo.model.Worker")
&& count == 0) {
System.out.println(table.toString());
count = 1;
}
org.hibernate.mapping.Column col =
new org.hibernate.mapping.Column();
col.setName(f.getColumnName());
org.hibernate.mapping.Column typeCol = processColumn(col, f);
col.setSqlType(typeCol.getSqlType());
col.setSqlTypeCode(typeCol.getSqlTypeCode());
value = new SimpleValue(mappings, hibTable);
Class<?> javaType;
if (col.getSqlTypeCode() != null)
{
javaType = setDefaultJavaType(col.getSqlTypeCode());
}
else
{
System.out.println("sqlCodeType is null!");
javaType = null;
}
String typeName;
if (javaType != null)
{
typeName = setHibernateType(javaType, col.getSqlTypeCode());
}
else
{
col.setNullable(true);
System.out.println( " Column is " + col.getName());
typeName = null;
}
value.setTypeName(typeName);
value.setTable(hibTable);
value.addColumn(col);
if (f.isPrimaryKey())
{
PrimaryKey pk = new PrimaryKey();
pk.setName(f.getColumnName());
pk.setTable(hibTable);
pk.addColumn(col);
hibTable.setIdentifierValue(value);
hibTable.setPrimaryKey(pk);
}
property = new Property();
property.setName(col.getName());
property.setValue(value);
property.setPersistentClass(clazz);
property.setNodeName(table.getObjectType().getName());
clazz.addProperty(property);
mappings.addColumnBinding(property.getName(),
col, hibTable);
}
System.out.println("PersistentClass is " +
clazz.getEntityName());
mappings.addClass(clazz);
mappings.addImport(clazz.getEntityName(),
clazz.getEntityName());
cfg.addAnnotatedClass(clazz.getClass());
cfg.buildMapping();
}
public org.hibernate.mapping.Column processColumn
(org.hibernate.mapping.Column col,
Column ojbColumn) {
if (ojbColumn.getJDBCType().equals("VARCHAR"))
{
col.setSqlType(ojbColumn.getJDBCType());
col.setSqlTypeCode(Types.VARCHAR);
}
else if (ojbColumn.getJDBCType().equals("BIGINT"))
{
col.setSqlType(ojbColumn.getJDBCType());
col.setSqlTypeCode(Types.BIGINT);
}
else if (ojbColumn.getJDBCType().equals("INTEGER"))
{
col.setSqlType(ojbColumn.getJDBCType());
col.setSqlTypeCode(Types.INTEGER);
}
else if (ojbColumn.getJDBCType().equals("DOUBLE"))
{
col.setSqlType(ojbColumn.getJDBCType());
col.setSqlTypeCode(Types.DOUBLE);
}
else if (ojbColumn.getJDBCType().equals("BIT"))
{
col.setSqlType(ojbColumn.getJDBCType());
col.setSqlTypeCode(Types.BOOLEAN);
}
return col;
}
public static Class<?> setDefaultJavaType(int jdbcType) {
switch (jdbcType) {
case Types.BIGINT:
return Long.class;
case Types.BIT:
return Boolean.class;
case Types.BOOLEAN:
return Boolean.class;
case Types.CHAR:
return String.class;
case Types.VARCHAR:
return String.class;
case Types.DATE:
return Date.class;
case Types.TIME:
return Time.class;
case Types.TIMESTAMP:
return Timestamp.class;
case Types.DECIMAL:
return BigDecimal.class;
case Types.NUMERIC:
return BigDecimal.class;
case Types.DOUBLE:
return Double.class;
case Types.REAL:
return Double.class;
case Types.FLOAT:
return Float.class;
case Types.INTEGER:
return Integer.class;
case Types.SMALLINT:
return Short.class;
case Types.TINYINT:
return Byte.class;
case Types.BINARY:
return byte[].class;
case Types.BLOB:
return java.sql.Blob.class;
case Types.CLOB:
return java.sql.Blob.class;
case Types.LONGVARBINARY:
return byte[].class;
case Types.LONGVARCHAR:
return java.lang.String.class;
case Types.VARBINARY:
return byte[].class;
case Types.ARRAY:
return java.sql.Array.class;
case Types.DATALINK:
return java.net.URL.class;
case Types.DISTINCT:
case Types.JAVA_OBJECT:
return String.class;
case Types.NULL:
case Types.OTHER:
case Types.REF:
return java.sql.Ref.class;
case Types.STRUCT:
return java.sql.Struct.class;
default:
System.out.println("Unsupported jdbc
type: {} " + jdbcType);
return null;
}
}
public static String setHibernateType(Class<?> javaType, int
sqlTypeCode) {
String typeName;
int jdbcTypeCode;
jdbcTypeCode = sqlTypeCode;
if (javaType == null) {
typeName = "TypeName Error";
return typeName;
}
if (javaType == Long.class) {
return typeName = LongType.INSTANCE.getName();
} else if (javaType == Short.class) {
return typeName = ShortType.INSTANCE.getName();
} else if (javaType == Integer.class) {
return typeName =
IntegerType.INSTANCE.getName();
} else if (javaType == Byte.class) {
return typeName = ByteType.INSTANCE.getName();
} else if (javaType == Float.class) {
return typeName = FloatType.INSTANCE.getName();
} else if (javaType == Double.class) {
return typeName = DoubleType.INSTANCE.getName();
} else if (javaType == Character.class) {
return typeName =
CharacterType.INSTANCE.getName();
} else if (javaType == String.class) {
return typeName = StringType.INSTANCE.getName();
} else if
(java.util.Date.class.isAssignableFrom(javaType)) {
switch (jdbcTypeCode) {
case Types.DATE:
typeName = DateType.INSTANCE.getName();
break;
case Types.TIME:
typeName = TimeType.INSTANCE.getName();
break;
case Types.TIMESTAMP:
typeName =
TimestampType.INSTANCE.getName();
break;
default:
typeName = null;
}
return typeName;
} else if (javaType == Boolean.class) {
if (jdbcTypeCode == Types.BIT || jdbcTypeCode ==
Types.BOOLEAN) {
return typeName =
BooleanType.INSTANCE.getName();
} else if (jdbcTypeCode == Types.NUMERIC ||
jdbcTypeCode == Types.DECIMAL) {
return typeName =
NumericBooleanType.INSTANCE.getName();
} else if (jdbcTypeCode == Types.CHAR ||
jdbcTypeCode == Types.VARCHAR) {
return typeName =
StringType.INSTANCE.getName();
} else {
return typeName = null;
}
} else if (javaType == BigDecimal.class) {
return typeName =
BigDecimalType.INSTANCE.getName();
} else if (javaType == BigInteger.class) {
return typeName =
BigIntegerType.INSTANCE.getName();
} else if (javaType == byte[].class) {
return typeName = BlobType.INSTANCE.getName();
}
return typeName = null;
}
}
--------------------------------------------
public class HibernateConfigurator {
private static Configuration hibConfig;
protected HibernateConfigurator() {}
public static Configuration getInstance()
{
if (hibConfig == null)
{
hibConfig = new Configuration();
hibConfig.configure();
}
return hibConfig;
}
}
--------------------------------------------
public class SessionFactoryUtil
{
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory()
throws HibernateException
{
try
{
Configuration configuration =
HibernateConfigurator.getInstance();
configuration.configure();
//configuration.buildMappings();
serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
//new Exception().printStackTrace();
sessionFactory =
configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e){ e.printStackTrace();}
return sessionFactory;
}
public static SessionFactory getInstance()
{
return configureSessionFactory();
}
public static void close()
{
if (sessionFactory != null) {
sessionFactory.close();
}
}
}