我正在尝试通过h2数据库进行Spring Security登录,我有两个实体:Users和UserRoles。他们通过一对多注释进行连接。
运行我的应用程序后,将创建UserRoles表和JoinTable,但不会创建表用户。
 用户实体:

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

最佳答案

1.尝试更改表格名称。

2.您可以更改属性

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


在控制台中检查SQL查询

3.尝试更改属性<property name="hibernate.hbm2ddl.auto">create</property>

希望这可以帮助!!

10-02 21:47