【问题标题】:Hibernate doesn't create an h2db tableHibernate 不创建 h2db 表
【发布时间】:2017-04-23 16:51:42
【问题描述】:

我正在尝试通过 h2 数据库进行 spring 安全登录,并且我有两个实体: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>

【问题讨论】:

  • Users 可能是保留字。
  • 我试过这个变种。其他名称也无法使用。

标签: java spring hibernate spring-security h2


【解决方案1】:

Hibernate 不允许创建带有“密码”列的表。重命名密码解决了这个问题。

【讨论】:

    【解决方案2】:

    1.尝试更改表名。

    2.可以更改属性

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

    在控制台中检查您的 sql 查询

    3.尝试更改属性&lt;property name="hibernate.hbm2ddl.auto"&gt;create&lt;/property&gt;

    希望这会有所帮助!

    【讨论】:

    • 错误:???µ???·???µ???,???‹?? ?,???? ???°?????‹?…: "0" 未知数据类型: "0"; SQL语句:创建表USERS(用户名varchar(255)不为空,启用1不为空,密码varchar(255)不为空,主键(用户名)) [50004-191]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2017-10-24
    • 2017-11-15
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多