2

I have a spring mvc application integrated with hibernate. I have 3 pojo which are marked for hibernate table creating.

  • Book.java with @Table(name = "BOOKS") .
  • User.java with @Table(name = "USER_TABLE")
  • Role.java with @Table(name = "ROLE_TABLE")

I have configured session factory and all.

My hibernate.hbm2ddl.auto = create-drop. But also tried create, update.

When I start my application BOOKS table is getting created in DB. but USER_TABLE and ROLE_TABLE is not getting created.

I get no exception.

When I do select * from USER_TABLE; the output is table or view doesn't exist.

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;}
}
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
user1834664
  • 443
  • 3
  • 12
  • 22
  • See this link: http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – DeepInJava Nov 05 '14 at 13:01
  • @Sanket: My issue is, out of 3 table which i want to create only books table is getting created, where as USER_TABLE and ROLE_TABLE is not getting created. I have tried all possible values for hibernate.hbm2ddl.auto. – user1834664 Nov 05 '14 at 13:10
  • show the complete logs generated by hibernate, I guess hibernate had an issue while creating the table itself so the table creation has failed. – Chaitanya Nov 05 '14 at 13:21
  • @user1834664 You need to follow either `annotation on fields` or `on methods` pattern to make you code consistent. *In BOOKS u have added annotations on fields & on other classes on methods.* – OO7 Nov 05 '14 at 13:22

1 Answers1

-1

There are some reasons, because your code will fail.

  1. Did you added the class file of ROLE_TABLE, USER_TABLE to the SessionFactory ?
  2. Place your annotations properly..

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

Replace this with:

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

You need to place the hibernate annotations before declaring the variable. I pasted your code in my app with my changes and it worked.

user3880352
  • 61
  • 1
  • 1
  • 7
  • Have u seen OP has added annotations on methods ? It is not mandatory to add annotations, if fields & DB columns are same named. – OO7 Nov 05 '14 at 13:16
  • All the pojo classes are placed in session factory. Placement of annotation can be at on variable declaration or on getter method. – user1834664 Nov 05 '14 at 13:18
  • In his/her code there are annotations at getter methods.. also the names of columns and fields are different.. – user3880352 Nov 05 '14 at 13:19
  • the annotations can be placed on fields or getter methods, so the code posted by OP is correct as the annotations are on getter methods. – Chaitanya Nov 05 '14 at 13:21
  • @user1834664 did you find the solutions? im stucked in the same problem. I know its a preatty old question. but it would be good if there is an answer to post it. – Daniel Henao Sep 17 '17 at 02:53