ids

时间:2016-10-21 13:25:03

标签: java hibernate

我从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);

ids是字符串,我将它们注释为@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代码,表和所有。是什么时候它在东西破坏时试图存储信息。

2 个答案:

答案 0 :(得分:11)

如果希望String成为数据库密钥,则需要使用UUID自动生成密钥。像这样:

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String myId;

答案 1 :(得分:0)

亲爱的,您使用字符串作为 id 的原因是它导致问题。将其设为 int 或 long 并检查数据库字段。以防其他人来寻找答案。

@GenericGenerator(name = "generator", strategy = "increment")
    @Id
    @GeneratedValue(generator = "generator")
    @Column(unique=true, nullable=false, precision=10,name="paymentid")
    private  Long  

这将解决您的问题