如何正确读取此异常?这是一个Hibernate映射问题吗?

时间:2016-10-24 14:46:56

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

我已经开始使用由另一个人启动的 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)]

我认为这只意味着在数据库表格中我的住宿表的 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中的错误取决于抛出到 mainSearchServicesImpl bean中的相同异常(由 placeSearcherController使用) )等等untile来到第一个抛出此异常的地方,即 AccomodationDAOImpl 实例(执行查询的地方)。

这种解释是否正确?

TNX

3 个答案:

答案 0 :(得分:2)

  

我认为somoene使用Long而其他人使用BigInteger。什么是最佳选择?

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

  

根据我的理解,这意味着placeSearcherController bean中的错误取决于抛出到mainSearchServicesImpl bean中的相同异常(由placeSearcherController使用)等等untile来到第一个引发此异常的地方,即AccomodationDAOImpl实例(执行查询的地方)。

     

这种解释是否正确?

是的,虽然它本身不是查询。 Hibernate验证它可以使用现有的db模式,但发现它不能。

答案 1 :(得分:1)

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

@Id
private Integer userId;

答案 2 :(得分:0)

我遇到了同样的问题。 请尝试定义 @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;