Papich
Papich

Reputation: 133

Hibernate doesn't create an h2db table

I'm trying to make a spring security login through h2 database and i have two entities: Users and UserRoles. They connected via one to many annotation. After i run my app, UserRoles table and JoinTable are created, but table users not. users entity:

Entity
@Table(name = "USERS")
public class Users {

    @Id
    @Column(name = "username", nullable = false)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "enabled", nullable = false, columnDefinition = "1")
    private int enabled;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "USER_ROLE", joinColumns = {
        @JoinColumn(name = "username")}, inverseJoinColumns = {
        @JoinColumn(name = "user_role_id")})
    private Set<UserRoles> userRoles = new HashSet<UserRoles>(0);
....
}

UserRoles entity:

@Entity
@Table(name = "USER_ROLES")
public class UserRoles {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_role_id")
    private int user_role_id;


    @Column(name = "role", nullable = false)
    private String role;
...
        }

hibernate.config.xml:

 <hibernate-configuration>
        <session-factory>
            <!-- Database connection settings -->
            <property name="connection.driver_class">org.h2.Driver</property>
            <property name="connection.url">jdbc:h2:file:d:\WebProjectDb</property>
            <property name="connection.username">sa</property>
            <property name="connection.password"></property>

            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>

            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.H2Dialect</property>

            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>

            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">false</property>

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

            <mapping class="com.webproject.Courses"/>
            <mapping class="com.webproject.Users"/>
            <mapping class="com.webproject.UserRoles"/>



        </session-factory>
    </hibernate-configuration>

Upvotes: 1

Views: 1698

Answers (2)

Papich
Papich

Reputation: 133

Hibernate doesn't allow to create a table with a "password" column. Rename to passworT solved the problem.

Upvotes: 0

karthik
karthik

Reputation: 17

1.Try changing the Table name.

2.You can change the property

<property name="show_sql">true</property>

to check your sql queries in console

3.Try changing the property <property name="hibernate.hbm2ddl.auto">create</property>

Hope this helps!!

Upvotes: 1

Related Questions