Hibernate 注释 - 无法映射日期

Hibernate annotations - date cannot be mapped

我有问题。我花了一个多小时在互联网上搜索,但我什么也没找到...... 我有一个简单的 Table class,它的元素之一是 java.util.Date 列表。当我运行程序时,显示异常:

> org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany
> targeting an unmapped class:    
> com.model.Time.timetable[java.util.Date].

我的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3036/test
        </property>
        <property name="hibernate.connection.username">
            root
        </property>

        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="com.model.Cinema" />
        <mapping class="com.model.Time" />

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

和我的 class:

package com.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import info.talacha.filmweb.models.Movie;



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

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private int id;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "time_id")
    private List<Date> timetable;

    @Column(name = "movie")
    private Movie movie;

    @Column(name = "dubbing")
    private boolean dubbing;

    @Column(name = "subtitles")
    private boolean subtitles;

    @Column(name = "threeDimensions")
    private boolean threeDimensions;



    public Time(){
        timetable = new ArrayList<Date>();
        dubbing= false;
        subtitles = false;
        threeDimensions = false;
        movie = new Movie();
    }



    public Time(int id, List<Date> timetable, Movie movie, boolean dubbing, boolean subtitles, boolean is3dMovie) {
        super();
        this.id = id;
        this.timetable = timetable;
        this.movie = movie;
        this.dubbing = dubbing;
        this.subtitles = subtitles;
        threeDimensions = is3dMovie;
    }



    public boolean isThreeDimensions() {
        return threeDimensions;
    }



    public void setThreeDimensions(boolean threeDimensions) {
        this.threeDimensions = threeDimensions;
    }



    public int getId() {
        return id;
    }



    public void setId(int id) {
        this.id = id;
    }


    public Movie getMovie() {
        return movie;
    }



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


    public Time(List<Date> timetable, Movie movie,boolean dubbing, boolean subtitles,boolean is3D) {
        this.timetable = timetable;
        this.dubbing = dubbing;
        this.subtitles = subtitles;
        this.movie = movie;
        this.threeDimensions = is3D;
    }

    public List<Date> getTimetable() {
        return timetable;
    }

    public void setTimetable(List<Date> timetable) {
        this.timetable = timetable;
    }

    public boolean isDubbing() {
        return dubbing;
    }

    public void setDubbing(boolean dubbing) {
        this.dubbing = dubbing;
    }

    public boolean isSubtitles() {
        return subtitles;
    }

    public void setSubtitles(boolean subtitles) {
        this.subtitles = subtitles;
    }


    @Override
    public String toString() {
        return "Time [timetable=" + timetable + ", movie=" + movie + ", dubbing=" + dubbing + ", subtitles="
                + subtitles + ", is3DMovie=" + threeDimensions + "]";
    }




}

当我将这种映射方式 (oneToMany) 用于不同类型时效果很好...我不知道哪里出了问题。我尝试了几件事,但没有用。我将感谢您的帮助!

尝试使用 java.sql.Date 中的日期属性来定义时间 class 的每个日期属性。

回见。

OneToMany 用于创建两个实体之间的关联。 java.util.Date 不是实体。这是一个基本类型。你要的是@ElementCollection.

尝试像这样使用@Temporal 注释:

@Temporal(value = TemporalType.TIMESTAMP)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "time_id")
private List<Date> timetable;