Hibernate不生成表

时间:2016-05-03 10:28:20

标签: java hibernate

当某些表添加到hibernate.cfg.xml时,Hibernate不生成任何表:

如果我添加:

<mapping class="org.hibernate.tutorial.annotations.Movie"/>
<mapping class="org.hibernate.tutorial.annotations.Actor"/>

根本没有生成表格,我已经有了:

<property name="hibernate.hbm2ddl.auto">update</property>

hibernate.cfg.xml中,我已经尝试了其中的变体,我知道问题在于这些类,因为其他类工作正常,并且在未添加这些类时会自动生成。

演员

package org.hibernate.tutorial.annotations;

import java.util.Set;

import javax.persistence.*;

@Entity
@Table(name="ACTOR")
public class Actor {
@Id @Column(name="ACTOR_ID")
private int id;

@Column(nullable=false, unique=true)
private String name;

@ManyToMany(
        targetEntity=Movie.class,
        cascade={CascadeType.PERSIST, CascadeType.MERGE}
    )
        @JoinTable(
            name="Actor_Movie",
            joinColumns=@JoinColumn(name="ACTOR_ID"),
            inverseJoinColumns=@JoinColumn(name="MOVIE_ID")
        )
private Set<Movie> films;

public Actor(){

}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Set<Movie> getFilms() {
    return films;
}
public void setFilms(Set<Movie> films) {
    this.films = films;
}
}

电影

import java.sql.Time;
import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name="MOVIE")
public class Movie {

@Id @Column(name="MOVIE_ID")
private int id;

@Column(nullable=false,unique=true)
private String title;

@ManyToMany(
    targetEntity=Track.class,
    cascade={CascadeType.PERSIST, CascadeType.MERGE},
    mappedBy = "movies"
)
private Set<Actor> actors;

private Time playTime;

public Movie(){

}

public int getId(){
    return id;
}

public String getTitle(){
    return title;
}

public Set<Actor> getActors(){
    return actors;
}

public Time getTime(){
    return playTime;
}

public void setTime(Time playTime){
    this.playTime = playTime;
}

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

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

public void setActors(Set<Actor> actors){
    this.actors = actors;
}
}

ResetDB

    import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import junit.framework.TestCase;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ResetDB extends TestCase{
    private SessionFactory sessionFactory;

    @Override
    protected void setUp() throws Exception {
        // A SessionFactory is set up once for an application
        sessionFactory = new Configuration()
        .configure() // configures settings from hibernate.cfg.xml
        .buildSessionFactory();
    }

    @Override
    protected void tearDown() throws Exception {
        if ( sessionFactory != null ) {
            sessionFactory.close();
        }
    }


    public void testResetDB() {

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        try {

            Query q = session.createSQLQuery("delete from ACTOR");
            q.executeUpdate();

            q = session.createSQLQuery("delete from MOVIE");
            q.executeUpdate();

            session.getTransaction().commit();

        } catch (Exception e) {
            System.out.println("Exception: "+e.getLocalizedMessage());
            System.out.println("Transaction rolled back");
            session.getTransaction().rollback();
        }

        session.close();
    }

}

使用hibernate-core-4.1.6

0 个答案:

没有答案