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

How correctly read this exception? Is it an Hibernate mapping issue?

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

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)]

我认为这仅意味着在数据库 table 我有一个 BigInt 值用于 idaccomodation table 但在 Accomodation class 映射此 table 我有:

@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 使用)等等,直到第一个抛出异常的地方是 AccomodationDAOImpl 实例(查询是执行)。

这个解释正确吗?

I fount that somoene use Long and someone else use BigInteger. What is the best choice?

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

From what I have understand it means that the error into the placeSearcherController bean depends by the same exception that is thrown into the mainSearchServicesImpl bean (used by placeSearcherController) and so on untile came to the first place where this exception was thrown that is the AccomodationDAOImpl instance (where the query is performed).

Is this interpretation correct?

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

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

@Id
private Integer userId;

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

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

示例:

对于 table。

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;