简体   繁体   中英

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:

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:

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:

@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? Reading online I fount that somoene use Long and someone else use 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 ).

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?

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

Compare them and decide based on your needs. Long is sufficient for most cases, but not all. The answer on this question is helpful: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?

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.

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

You can get the complete detail for this error -> 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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