簡體   English   中英

如何修復休眠的hbm.xml文件以使復合主鍵正常工作? (MappingException)

[英]How do I fix my hibernate hbm.xml file to make my composite primary keys work? (MappingException)

GitHub示例演示問題

https://github.com/jl431/Example.git

重現錯誤所需的所有代碼都在這里。

還需要啟動mysql服務器並更改休眠文件中的連接詳細信息。

錯誤

Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: beans.User, at table: RATINGS, for columns: [org.hibernate.mapping.Column(USER_ID)]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at simple.HibernateUtil.buildSessionFactory(HibernateUtil.java:26)
    at simple.HibernateUtil.<clinit>(HibernateUtil.java:10)
    at simple.MovieManager.<init>(MovieManager.java:9)
    at simple.MovieManager.main(MovieManager.java:13)
Caused by: org.hibernate.MappingException: Could not determine type for: beans.User, at table: RATINGS, for columns: [org.hibernate.mapping.Column(USER_ID)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
    at org.hibernate.tuple.PropertyFactory.buildStandardProperty(PropertyFactory.java:353)
    at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:71)
    at org.hibernate.mapping.Component.getType(Component.java:180)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:271)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
    at simple.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
    ... 3 more

這是我認為存在問題的一些關鍵文件。 其余代碼可從上面的github repo url獲得。

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/testingbeta</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>


        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="Movie.hbm.xml"/>
        <mapping resource="User.hbm.xml"/>
        <mapping resource="Rating.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

Rating.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="beans">
    <class name="Rating" table="RATINGS">
        <composite-id name="RatingId" class="RatingId">
            <key-property name="user" column="USER_ID" />
            <key-property name="movie" column="MOVIE_ID" />
        </composite-id>

        <property name="rating" />
    </class>
</hibernate-mapping>

Rating.java

package beans;

import java.util.Date;

public class Rating {
    private RatingId ratingId;
    private int rating;
    private Date timestamp;

    public Rating() {}  // No arg contructor for hibernate

    public Rating(RatingId ratingId, int rating) {
        super();
        this.ratingId = ratingId;
        this.rating = rating;
    }

    public RatingId getRatingId() {
        return ratingId;
    }

    public void setRatingId(RatingId ratingId) {
        this.ratingId = ratingId;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }
}

RatingId.java

package beans;

import java.io.Serializable;

public class RatingId implements Serializable {
    private User user;
    private Movie movie;

    public RatingId(User user, Movie movie) {
        this.user = user;
        this.movie = movie;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Movie getMovie() {
        return movie;
    }

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((movie == null) ? 0 : movie.hashCode());
        result = prime * result + ((user == null) ? 0 : user.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RatingId other = (RatingId) obj;
        if (movie == null) {
            if (other.movie != null)
                return false;
        } else if (!movie.equals(other.movie))
            return false;
        if (user == null) {
            if (other.user != null)
                return false;
        } else if (!user.equals(other.user))
            return false;
        return true;
    }
}

我懷疑組合鍵列是否可以在不同的表中(在我們的情況下-USERS和MOVIE表)。 據我了解,它們需要位於RATINGS表中。 我嘗試將它們映射到同一張表中,並且能夠成功保留評級對象。 我正在使用JPA批注。 帶注釋的配置非常簡單。 下面是代碼。

注-組合鍵是使用JPA中的@Embeddeble@EmbeddedId注釋定義的。

@Embeddable
public class User {

    @Column(name = "USER_ID")
    private long userId;
    @Column(name = "EMAIL_ADDRESS")
    private String email;
    public User() {}

    public User(long userId, String email, String password) {
        this.userId = userId;
        this.email = email;
    }
    public long getUserId() {
        return userId;
    }
    public void setUserId(long userId) {
        this.userId = userId;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

@Embeddable
public class Movie {
    @Column(name = "MOVIE_ID")
    private long movieId;

    @Column(name = "MOVIE_NAME")
    private String title;

    public Movie() {}
    public Movie(long movieId, String title) {
        this.movieId = movieId;
        this.title = title;
    }
    public long getMovieId() {
        return movieId;
    }
    public void setMovieId(long movieId) {
        this.movieId = movieId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

@Embeddable
public class RatingId implements Serializable {

    @Embedded
    private User user;  //Embeddable inside Embeddable.
    @Embedded
    private Movie movie;

    public RatingId() {
    }

    public RatingId(User user, Movie movie) {
        this.user = user;
        this.movie = movie;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Movie getMovie() {
        return movie;
    }

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((movie == null) ? 0 : movie.hashCode());
        result = prime * result + ((user == null) ? 0 : user.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RatingId other = (RatingId) obj;
        if (movie == null) {
            if (other.movie != null)
                return false;
        } else if (!movie.equals(other.movie))
            return false;
        if (user == null) {
            if (other.user != null)
                return false;
        } else if (!user.equals(other.user))
            return false;
        return true;
    }
}

@Entity
@Table(name = "RATING")
public class Rating {

    @EmbeddedId
    private RatingId ratingId;
    private int rating;
    private Date timestamp;

    public Rating() {}  // No arg contructor for hibernate

    public Rating(RatingId ratingId, int rating) {
        super();
        this.ratingId = ratingId;
        this.rating = rating;
    }

    public RatingId getRatingId() {
        return ratingId;
    }

    public void setRatingId(RatingId ratingId) {
        this.ratingId = ratingId;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }
}

調用entityManager.persist(rating)將評級對象持久保存在db中。 總而言之,數據庫中只有一個表-RATINGS具有所有必需的屬性-用戶,電影和評分。 那也是你想像的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM