2014-11-05 110 views
1

我有一個與hibernate集成的spring mvc應用程序。 我有3個標記爲hibernate表格創建的pojo。休眠不創建oracle中的表db

  • Book.java with @Table(name =「BOOKS」)。
  • User.java與@Table(NAME = 「USER_TABLE」)
  • Role.java與@Table(NAME = 「ROLE_TABLE」)

我已經配置會話工廠和所有。我的hibernate.hbm2ddl.auto = create-drop。但也試過create, update

當我開始我的應用程序BOOKS表正在DB中創建。但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;} 
} 
+0

請參閱此鏈接:http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – DeepInJava 2014-11-05 13:01:48

+0

@Sanket:我的問題是,出3張表我哪隻想創建書本表正在創建,其中USER_TABLE和ROLE_TABLE未創建。我已經嘗試了hibernate.hbm2ddl.auto的所有可能的值。 – user1834664 2014-11-05 13:10:14

+0

顯示由hibernate生成的完整日誌,我猜hibernate在創建表本身時有問題,所以表創建失敗。 – Chaitanya 2014-11-05 13:21:06

回答

0

有一些原因,因爲你的代碼會失敗。

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

您需要聲明變量之前放置休眠註釋。 我在我的應用程序中粘貼了您的代碼並進行了更改,並且工作正常。

+0

你有沒有看到OP在方法上添加了註釋?如果字段和數據庫列名稱相同,則不一定要添加註釋。 – OO7 2014-11-05 13:16:12

+0

所有pojo類放置在會話工廠。 註釋的放置可以在變量聲明或getter方法上。 – user1834664 2014-11-05 13:18:27

+0

在他/她的代碼中有getter方法的註釋..列和字段的名稱也是不同的.. – user3880352 2014-11-05 13:19:26