【问题标题】:How correctly read this exception? Is it an Hibernate mapping issue?如何正确阅读此异常?是 Hibernate 映射问题吗?
【发布时间】:2017-03-06 09:39:56
【问题描述】:

我已经开始使用另一个人启动的 Hibernate 应用程序开发一个新的 Spring Boot,我遇到以下问题:在应用程序启动期间,我收到以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'placeSearcherController': Unsatisfied dependency expressed through field 'mainSearchServices'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainSearchServicesImpl': Unsatisfied dependency expressed through field 'accomodationDAO'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accomodationDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/betrivius/config/DatabaseConfig.class]: Invocation of init method failed; 
nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [id] in table [accomodation]; found [bigint (Types#BIGINT)], but expecting [integer (Types#INTEGER)]

所以,这个异常链的最后一个例子是:

nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [id] in table [accomodation]; found [bigint (Types#BIGINT)], but expecting [integer (Types#INTEGER)]

我认为这仅意味着在数据库表上我对 accomodation 表的 id 列有一个 BigInt 值,但在映射此表的 Accomodation 类:

@Entity
@Table(name = "accomodation")
public class Accomodation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    ..........................................................
    ..........................................................
    ..........................................................
}

这是问题吗? 那么 MySql BigInt 数据类型的正确 Java 类型是什么?在线阅读我发现somoene 使用Long,而其他人使用BigInteger。最好的选择是什么?

另一个重要的疑问是关于如何正确阅读前面的异常链:

它首先在 placeSearcherController bean 中显示一个错误(它被抛出一个 UnsatisfiedDependencyException)。

据我了解,这意味着 placeSearcherController bean 中的错误取决于 ma​​inSearchServicesImpl bean 中抛出的相同异常(由 placeSearcherController 使用) 以此类推,直到抛出此异常的第一个位置,即 AccomodationDAOImpl 实例(执行查询的位置)。

这个解释正确吗?

【问题讨论】:

    标签: java spring hibernate spring-boot spring-data


    【解决方案1】:

    我发现有些人使用 Long 而其他人使用 BigInteger。最好的选择是什么?

    比较它们并根据您的需要决定。对于大多数情况来说,Long 就足够了,但不是全部。这个问题的答案很有帮助:Long vs BigInteger

    据我了解,这意味着 placeSearcherController bean 中的错误取决于抛出到 mainSearchServicesImpl bean 中的相同异常(由 placeSearcherController 使用),依此类推,直到出现此异常的第一个位置,即AccomodationDAOImpl 实例(执行查询的位置)。

    这个解释正确吗?

    是的,虽然它本身不是一个查询。 Hibernate 正在验证它是否可以使用现有的数据库模式,但发现它不能。

    【讨论】:

      【解决方案2】:

      我遇到了这个问题,原因是实体的主键是原始类型。通过将其更改为包装器,我的问题得到了解决。

      @Id
      private Integer userId;
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。 请尝试定义 @ColumnDefinition

        您可以获得此错误的完整详细信息 -> HERE

        示例:

        餐桌。

        CREATE TABLE event (
            id NUMERIC(19,0) IDENTITY NOT NULL, 
            PRIMARY KEY (id)
        )
        

        实体将是。

        @Id
        @GeneratedValue(
            strategy = GenerationType.IDENTITY
        )
        @Column(
            columnDefinition = "NUMERIC(19,0)"
        )
        private Long id;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-11
          • 2018-02-24
          • 2020-04-01
          • 2012-05-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多