2016-12-08 3 views
1

Ich versuche, eine Spring Security-Anmeldung über h2-Datenbank zu machen, und ich habe zwei Entitäten: Benutzer und UserRoles. Sie waren über eine Annotation verbunden. Nachdem ich meine App ausgeführt habe, werden UserRoles-Tabelle und JoinTable erstellt, aber Tabellenbenutzer nicht. Benutzer Einheit:Hibernate erstellt keine h2db-Tabelle

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 Einheit:

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

Benutzer ist wahrscheinlich ein reserviertes Wort. –

+0

Ich habe diese Variante ausprobiert. Nichts funktioniert auch mit anderen Namen. – Papich

Antwort

0

In Hibernate kann keine Tabelle mit einer Spalte "password" erstellt werden. Umbenennen zu PassworT löste das Problem.

1

1.Try den Tabellennamen zu ändern.

2.You kann die Eigenschaft

<property name="show_sql">true</property> 
ändern Sie Ihre SQL-Abfragen in der Konsole zu überprüfen

3.Try Ändern der Eigenschaft <property name="hibernate.hbm2ddl.auto">create</property>

hoffe, das hilft

!!

+0

Fehler: ??? μ ??? ???? ??? ???? ??? ?, ???? ??? ° ????? Papich