简体   繁体   English

如何正确读取此异常? 这是休眠映射问题吗?

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

I have started to work on a new Spring Boot using Hibernate application started by another person and I have the following problem: during the application startup I obtain the following error:我已经开始使用由另一个人启动的 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)]

So, the last exacption into this exceptions chain is:所以,这个异常链的最后一个 exacption 是:

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

I think that it only means that on the database table I have a BigInt value for the id column of the accomodation table but on the Accomodation class that maps this table I have:我认为这仅意味着在数据库表上我有一个BigInt值用于住宿表的id列,但在映射此表的住宿类上我有:

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

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

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

Is it the problem?这是问题吗? So what is the correct Java type for MySql BigInt data type?那么,MySql BigInt数据类型的正确 Java 类型是什么? Reading online I fount that somoene use Long and someone else use BigInteger .在线阅读我发现 somoene 使用Long而其他人使用BigInteger What is the best choice?什么是最好的选择?

Another important doubt is related about how to correctly read the previous excepeptions chain:另一个重要的疑问与如何正确读取前面的异常链有关:

It first show an error into the placeSearcherController bean (it is thrown an UnsatisfiedDependencyException ).它首先在placeSearcherController bean 中显示一个错误(它被抛出一个UnsatisfiedDependencyException )。

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).据我所知,这意味着placeSearcherController bean 中的错误取决于抛出到mainSearchServicesImpl bean(由placeSearcherController 使用)中的相同异常,依此类推,直到第一个抛出此异常的地方,即AccomodationDAOImpl实例(执行查询的地方)。

Is this interpretation correct?这个解释正确吗?

I fount that somoene use Long and someone else use BigInteger.我发现 somoene 使用 Long 而其他人使用 BigInteger。 What is the best choice?什么是最好的选择?

Compare them and decide based on your needs.比较它们并根据您的需求做出决定。 Long is sufficient for most cases, but not all.对于大多数情况,Long 就足够了,但不是全部。 The answer on this question is helpful:Long vs BigInteger这个问题的答案很有帮助: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).据我所知,这意味着 placeSearcherController bean 中的错误取决于抛出到 mainSearchServicesImpl bean(由 placeSearcherController 使用)中的相同异常,依此类推,直到第一个抛出此异常的地方,即 AccomodationDAOImpl 实例(执行查询的地方)。

Is this interpretation correct?这个解释正确吗?

Yes, though it's not a query per se.是的,虽然它本身不是查询。 Hibernate is validating that it can work with the existing db schema, and finding it cannot. Hibernate 正在验证它可以与现有的 db 模式一起工作,但发现它不能。

I had this problem and the cause was having the entity's primary key in a primitive type.我遇到了这个问题,原因是实体的主键是原始类型。 By changing it to a wrapper, my problem was solved.通过将其更改为包装器,我的问题解决了。

@Id
private Integer userId;

I was having the same issue.我遇到了同样的问题。 Please try defining @ColumnDefinition请尝试定义@ColumnDefinition

You can get the complete detail for this error -> HERE您可以获取此错误的完整详细信息 -> HERE

Example:示例:

for table.为表。

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

Entity will be.实体会。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM