【问题标题】:Hibernate not creating table in oracle dbHibernate 不在 oracle db 中创建表
【发布时间】:2015-01-01 16:15:38
【问题描述】:

我有一个与 hibernate 集成的 spring mvc 应用程序。 我有 3 个标记为用于创建休眠表的 pojo。

  • Book.java 与 @Table(name = "BOOKS") 。
  • 带有@Table(name = "USER_TABLE") 的User.java
  • Role.java 与 @Table(name = "ROLE_TABLE")

我已经配置了会话工厂等等。

我的hibernate.hbm2ddl.auto = create-drop。但也试过create, update

当我启动我的应用程序时,BOOKS 表正在数据库中创建。但是 USER_TABLE 和 ROLE_TABLE 没有被创建。

我得到没有例外

当我做select * from USER_TABLE; 输出表或视图不存在

package com.ca.service.form;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
* @author jamju02
*/
@Entity
@Table(name = "ROLE_TABLE")
public class Role {
    private long id;
    private String roleName;

    private Set<User> users = new HashSet<User>();

    /**
     * @return the id
     */
     @Id
     @GeneratedValue
     @Column(name = "ROLE_ID")
    public long getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }
    /**
     * @return the roleName
     */
    public String getRoleName() {
        return roleName;
    }
    /**
     * @param roleName the roleName to set
     */
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    /**
     * @return the users
     */
     @ManyToMany(cascade = CascadeType.ALL)
     @JoinTable(
            name = "USERS_ROLES_TABLE",
            joinColumns = @JoinColumn(name = "ROLE_ID"),
            inverseJoinColumns = @JoinColumn(name = "USER_ID")
     )
    public Set<User> getUsers() {
        return users;
    }
    /**
     * @param users the users to set
     */
    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

package com.ca.service.form;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
* @author jamju02
*/
@Entity
@Table(name = "USER_TABLE")
public class User {
    private long id;
    private String username;
    private String password;
    private String email;

    private Set<Role> roles = new HashSet<Role>();

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    // public void addRole(Role role) {
    // this.roles.add(role);
    // }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue
    @Column(name = "USER_ID")
    public long getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email
     *            the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

     /**
     * @return the roles
     */
     @ManyToMany(mappedBy = "users")
     public Set<Role> getRoles() {
         return roles;
     }

     /**
     * @param roles the roles to set
     */
     public void setRoles(Set<Role> roles) {
         this.roles = roles;
     }
}


package com.ca.service.form;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "BOOKS")
public class Book {

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="BOOK_NAME")
    private String bookName;

    @Column(name="AUTHOR")
    private String author;

    @Column(name="PRICE")
    private int price;

    @Column(name="QTY")
    private int quantity;

    public Integer getId() 
    {return id;}

    public String getBookName() 
    {return bookName;}

    public String getAuthor() 
    {return author;}

    public int getPrice() 
    {return price;}

    public int getQuantity() 
    {return quantity;}

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

    public void setBookName(String bookName) 
    {this.bookName = bookName;}

    public void setAuthor(String author) 
    {this.author = author;}

    public void setPrice(int price) 
    {this.price = price;}

    public void setQuantity(int quantity) 
    {this.quantity = quantity;}
}

【问题讨论】:

  • @Sanket:我的问题是,在我想创建的 3 个表中,只创建了 books 表,而没有创建 USER_TABLE 和 ROLE_TABLE。我已经尝试了 hibernate.hbm2ddl.auto 的所有可能值。
  • 显示hibernate生成的完整日志,我猜是hibernate在创建表时遇到了问题,所以创建表失败了。
  • @user1834664 您需要遵循annotation on fieldson methods 模式以使您的代码保持一致。 在 BOOKS 中,您在字段和方法的其他类上添加了注释。

标签: java spring oracle hibernate


【解决方案1】:

有一些原因,因为你的代码会失败。

  1. 您是否将ROLE_TABLE、USER_TABLE的类文件添加到SessionFactory?
  2. 正确放置注释..

    @Entity
    @Table(name = "ROLE_TABLE")
    public class Role {
    private long id;
    private String roleName;
    
    private Set<User> users = new HashSet<User>();
    
    /**
    * @return the id
    */
    @Id
    @GeneratedValue
    @Column(name = "ROLE_ID")
    public long getId() {
        return id;
    }
    

替换为:

@Entity
@Table(name = "ROLE_TABLE")
public class Role {
 @Id @GeneratedValue
 @Column(name = "ROLE_ID")
 private long id;
 private String roleName;

 private Set<User> users = new HashSet<User>();

 /**
  * @return the id
  */

 public long getId() {
   return id;
 }

您需要在声明变量之前放置 hibernate 注释。 我将您的代码与我的更改一起粘贴到了我的应用程序中,并且成功了。

【讨论】:

  • 你见过 OP 在方法上添加了注释吗?如果字段和数据库列名称相同,则不需要添加注释。
  • 所有 pojo 类都放在会话工厂中。注释的位置可以在变量声明或 getter 方法上。
  • 在他/她的代码中,getter方法有注释..列和字段的名称也不同..
  • 注解可以放在字段或getter方法上,所以OP贴的代码是正确的,因为注解在getter方法上。
  • @user1834664 你找到解决方案了吗?我陷入了同样的问题。我知道这是一个非常古老的问题。但是如果有答案发布它会很好。
猜你喜欢
  • 2020-03-22
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2017-10-24
相关资源
最近更新 更多