Hibernate ID的未知整数数据类型

bfhwhh0e  于 2023-01-31  发布在  其他
关注(0)|答案(4)|浏览(173)

我从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有级联注解,因此应该正确Map。
我已经尝试了没有www.example.com行的代码,没有抛出错误,所以这不是代码本身的问题,它必须与Hibernate的东西。session.save line and doesn't throw errors, so is not a problem in the code per se, it has to be something with 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代码,表和所有。是当它试图存储信息时,事情打破了。

3zwjbxry

3zwjbxry1#

亲爱的,你使用了字符串作为ID,这是为什么它会导致问题。使它成为整型或长整型,并检查数据库字段以及。在情况下,其他人来寻找答案。

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

这将解决您的问题

vom3gejh

vom3gejh2#

对于未来的读者,尤其是像我这样刚到Spring的人,我也遇到了同样的问题,尽管我的ID是Long类型。
最初,我在注解示例变量之前创建了示例变量,然后在所有示例变量的to处创建了注解,如下所示。

public class Student {
    @Id
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            generator = "student_sequence",
            strategy = GenerationType.SEQUENCE
    )
    private String name;
    private Long id;
    private String email;

在阅读了AamirKhan对这篇文章的回答后,我才意识到这个问题,实际上我是在注解名称(类型为String)而不是ID,因此出现了错误。
从下面的解决方案中可以看出,将id放在注解的正下方和名称的正上方解决了这个问题。

public class Student {
    @Id
    @SequenceGenerator(
            name = "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            generator = "student_sequence",
            strategy = GenerationType.SEQUENCE
    )
    private Long id;
    private String name;
    private String email;
zvokhttg

zvokhttg3#

如果你希望String作为数据库键,你需要使用UUID来自动生成键。

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

fzsnzjdm4#

我在使用Postgres时遇到这个错误。我使用字符串作为我的ID。就像这样:

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private String id;

因此我删除了@GeneratedValue注解,因为它与spring不匹配。

@Id

private String id;

那就成功了!

相关问题