org.hibernate.HibernateException:缺少列:genre_id,但我注释了它" genreId"

时间:2015-04-17 10:17:04

标签: java spring hibernate

我对hibernate有一些奇怪的错误。它在例外中说明以下内容:

引起:org.hibernate.HibernateException:缺少列:movietime2.genres中的genre_id

但是,我这样注释了GenresEntity的@Id列:

    @Id
    @Column(name = "genreId", nullable = false, insertable = true, updatable = true)
    public int getGenreId() {
        return genreId;
    }

Here is the database table.

GenresEntity类代码:

package com.movietime.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;

/**
 * Created by Attila on 2015-04-16.
 */
@Entity
@Table(name = "genres", schema = "", catalog = "movietime2")
public class GenresEntity {
    private int movieid;
    private String genre;
    private int genreId;
    @JsonIgnore
    private MoviesEntity movie;

    @Basic
    @Column(name = "movieid", nullable = false, insertable = false, updatable = false)
    public int getMovieid() {
        return movieid;
    }

    public void setMovieid(int movieid) {
        this.movieid = movieid;
    }

    @Basic
    @Column(name = "genre", nullable = false, insertable = true, updatable = true, length = 50)
    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    @Id
    @Column(name = "genreId", nullable = false, insertable = true, updatable = true)
    public int getGenreId() {
        return genreId;
    }

    public void setGenreId(int genreId) {
        this.genreId = genreId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        GenresEntity that = (GenresEntity) o;

        if (genreId != that.genreId) return false;
        if (movieid != that.movieid) return false;
        if (genre != null ? !genre.equals(that.genre) : that.genre != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = movieid;
        result = 31 * result + (genre != null ? genre.hashCode() : 0);
        result = 31 * result + genreId;
        return result;
    }

    @ManyToOne
    @JoinColumn(name = "movieid", referencedColumnName = "movieid", nullable = false)
    public MoviesEntity getMovie() {
        return movie;
    }

    public void setMovie(MoviesEntity movie) {
        this.movie = movie;
    }
}

MoviesEntitiy class:

package com.movietime.model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import javax.persistence.*;
import java.util.List;

/**
 * Created by Attila on 2015-04-06.
 */
@Entity
@Table(name = "movies", schema = "", catalog = "movietime2")
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "movieid")
public class MoviesEntity {
    private int movieid;
    private String title;
    private String year;
    private String imdbid;
    //@JsonManagedReference
    @JsonIgnore
    private List<ActorsEntity> actors;
    //@JsonManagedReference
    @JsonIgnore
    private List<WritersEntity> writers;
    //@JsonManagedReference
    @JsonIgnore
    private List<ProducersEntity> producers;
    @JsonIgnore
    private List<Movies2ActorsEntity> characters;
    @JsonIgnore
    private MpaaratingsEntity mpaaRating;
    @JsonIgnore
    private List<GenresEntity> genres;


    @Id
    @Column(name = "movieid", nullable = false, insertable = true, updatable = true)
    public int getMovieid() {
        return movieid;
    }

    public void setMovieid(int movieid) {
        this.movieid = movieid;
    }

    @Basic
    @Column(name = "title", nullable = false, insertable = true, updatable = true, length = 400)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Basic
    @Column(name = "year", nullable = true, insertable = true, updatable = true, length = 100)
    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    @Basic
    @Column(name = "imdbid", nullable = true, insertable = true, updatable = true, length = 10)
    public String getImdbid() {
        return imdbid;
    }

    public void setImdbid(String imdbid) {
        this.imdbid = imdbid;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MoviesEntity that = (MoviesEntity) o;

        if (movieid != that.movieid) return false;
        if (imdbid != null ? !imdbid.equals(that.imdbid) : that.imdbid != null) return false;
        if (title != null ? !title.equals(that.title) : that.title != null) return false;
        if (year != null ? !year.equals(that.year) : that.year != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = movieid;
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (year != null ? year.hashCode() : 0);
        result = 31 * result + (imdbid != null ? imdbid.hashCode() : 0);
        return result;
    }

    //@ManyToMany(fetch = FetchType.LAZY)
    //@JoinTable(name = "movies2actors", catalog = "movietime2", schema = "", joinColumns = @JoinColumn(name = "movieid", referencedColumnName = "movieid", nullable = false), inverseJoinColumns = @JoinColumn(name = "actorid", referencedColumnName = "actorid", nullable = false))
    @Transient
    public List<ActorsEntity> getActors() {
        return actors;
    }

    public void setActors(List<ActorsEntity> actors) {
        this.actors = actors;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "movies2writers", catalog = "movietime2", schema = "",
            joinColumns = @JoinColumn(name = "movieid", referencedColumnName = "movieid", nullable = false),
            inverseJoinColumns = @JoinColumn(name = "writerid", referencedColumnName = "writerid", nullable = false))
    public List<WritersEntity> getWriters() {
        return writers;
    }

    public void setWriters(List<WritersEntity> writers) {
        this.writers = writers;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "movies2producers", catalog = "movietime2", schema = "", joinColumns = @JoinColumn(name = "movieid", referencedColumnName = "movieid", nullable = false), inverseJoinColumns = @JoinColumn(name = "producerid", referencedColumnName = "producerid", nullable = false))
    public List<ProducersEntity> getProducers() {
        return producers;
    }

    public void setProducers(List<ProducersEntity> producers) {
        this.producers = producers;
    }

    @OneToMany(mappedBy = "movie")
    public List<Movies2ActorsEntity> getCharacters() {
        return characters;
    }

    public void setCharacters(List<Movies2ActorsEntity> characters) {
        this.characters = characters;
    }

    @OneToOne
    @JoinColumn(name = "movieid", referencedColumnName = "movieid", nullable = false)
    public MpaaratingsEntity getMpaaRating() {
        return mpaaRating;
    }

    public void setMpaaRating(MpaaratingsEntity mpaaRating) {
        this.mpaaRating = mpaaRating;
    }

    @OneToMany(mappedBy = "movie")
    public List<GenresEntity> getGenres() {
        return genres;
    }

    public void setGenres(List<GenresEntity> genres) {
        this.genres = genres;
    }
}

为什么要搜索genre_id列?

3 个答案:

答案 0 :(得分:2)

Hibernate希望看到一个名为genre_id的列,然后在它无法在您的数据库中找到它时吐出芯片。这是因为重命名策略。您可以通过设置重命名策略来解决此问题,例如在使用Spring JPA时这样做:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy

您可以在&#39; hibernate.cfg.xml`(我认为是)中实现相同的目标:

<property name="hibernate.naming_strategy">org.hibernate.cfg.DefaultComponentSafeNamingStrategy</property>

答案 1 :(得分:0)

您的例外:

Caused by: org.hibernate.HibernateException: Missing column: genre_id in movietime2.genres
    at org.hibernate.mapping.Table.validateColumns(Table.java:365)

来自桌面类的Hibernate源:

ColumnMetadata columnInfo = tableInfo.getColumnMetadata( col.getName() );
if ( columnInfo == null ) {
    throw new HibernateException( "Missing column: " + col.getName() + " in "  
     + Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()));
}

您班级失败的可能性可能是由于以下原因之一:

  1. 您正在连接错误的数据库。
  2. 您的正在运行的课程不包含您所做的更改。您需要检查您的类是否已正确编译。

答案 2 :(得分:0)

好的,我决定采用一种简单的解决方法。我将数据库表中的genreId列重命名为genre_id,我也更改了Java注释中的引用,现在它可以工作了。所以没有好的解决方案,但至少我可以继续我的工作。