我有一个与hibernate集成的spring mvc应用程序。 我有3个pojo,标记为hibernate表创建。
我已经配置了会话工厂和所有。
我的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 :(得分:-1)
有一些原因,因为您的代码会失败。
正确放置注释..
@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注释。 我将我的代码粘贴到我的应用程序中,并且有效。