Hibernate注释 - 无法映射日期

时间:2015-01-02 16:09:53

标签: java spring hibernate pojo

我遇到了问题。我花了一个多小时在互联网上搜索,但我什么也没发现...... 我有一个简单的Table类,其中一个元素是java.util.Date的List。当我运行程序时,会显示异常:

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

和我的班级:

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)工作得很好......我不知道什么是错的。我尝试了一些东西,但他们没有工作。我将非常感谢你的帮助!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:-1)

尝试使用java.sql.Date中的Date attribut来定义Time类的每个Date attributs。

见。