我从Hibernate开始,但有一个我无法弄清的错误。
我有以下课程:
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class AbstractColumn { @Id @GeneratedValue(strategy = GenerationType.AUTO) private String id; private String name; //Other stuff }
那我有
@Entity public class DoubleColumn extends AbstractColumn implements Column { @ElementCollection private Map<Double,String> isNA; private double min=0; private double max=0; @ElementCollection private List<Double> data; // a lot of stuff }
最后:
@Entity public class DataFrame { @OneToMany(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private List<AbstractColumn> data; private String name; @Id @GeneratedValue private String id; @ElementCollection private Map<String,Integer> colIndex; //more stuff }
我得到的错误是:
Exception in thread "main" org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String at org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder(IdentifierGeneratorHelper.java:224) at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:98) at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40) at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:432) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:675) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:667) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:662) at Main.main(Main.java:285)
错误引发的唯一提示是该错误在此处的主类中:
DoubleColumn c1 = new DoubleColumn("Datos varios"); c1.addData(12); c1.addData(11); c1.addData(131); c1.addData(121); c1.addData(151); c1.addData(116); DataFrame datosHibernate = new DataFrame("Dataframe Hibernate"); datosHibernate.addColumn(c1); Configuration hibernateConfig = new Configuration(); SessionFactory sessionFactory = hibernateConfig.configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(datosHibernate); session.getTransaction().commit(); session.disconnect(); session.close(); System.exit(0);
这些id是字符串,我将它们标注为@GeneratedValue(我认为我不需要自己初始化它们)。关系@OneToMany具有级联注释,因此应正确映射。
我已经尝试了没有session.save行的代码,并且不会抛出错误,因此代码本身不是问题,它必须与Hibernate兼容。
我的配置文件是:
<hibernate-configuration> <session-factory> <property name="connection.url">jdbc:h2:E:/bd;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> <property name="connection.driver_class">org.h2.Driver</property> <property name="connection.username">user</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.H2Dialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping class="com.dataframe.estructuras.DataFrame"></mapping> <mapping class="com.dataframe.estructuras.column.AbstractColumn"/> <mapping class="com.dataframe.estructuras.column.types.DoubleColumn"/> </session-factory> </hibernate-configuration>
它创建了数据库文件,我看到了生成的所有SQL代码,表以及全部。是当事情中断时它尝试存储信息的时间。
如果要将String用作数据库密钥,则需要使用UUID自动生成密钥。像这样:
@Id @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy = "uuid") private String myId;