有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法映射java Hibernate批注日期

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

> 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)在我将其用于不同类型时非常有效。。。我不知道怎么了。我试了几次,但都没用。我将感谢你的帮助


共 (3) 个答案

  1. # 1 楼答案

    尝试使用@Temporal注释,如下所示:

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

    尝试使用java中的日期属性。sql。Date来定义Time类的每个日期属性

    再见

  3. # 3 楼答案

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