Composite Key with Hibernate

后端 未结 4 754
死守一世寂寞
死守一世寂寞 2021-02-15 06:45

In order to generate the next SQL code:

create table users (
    user_name varchar(15) not null primary key, 
    user_pass varchar(15) not null);

create table         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 07:32

    Maybe in fact something like this:

    
      
      
      
        
        
      
    
    

    This will map the roles to a collection in the User class, and roughly matches the schema you gave. It won't generate a primary key across the columns in user_roles though.

    You'll generally get better results from Hibernate with more normalised schemas though. For instance, in this case, you don't have a table for roles themselves; but what you seem to be describing would fairly naturally be modelled as a User and Role class, with a many-to-many relationship between them. Something like this:

    
      
      
      
        
        
      
    
    
      
    
    

    Now this will produce a schema with tables users, roles and user_roles and now user_roles will have a primary key across the user name and role name column. You also now have somewhere to hang additional information about roles, such as descriptions.

    Also, the error you're posting is a ClassNotFoundException which seems unlikely to be an actual problem with your mapping as such.

    You can do this using a composite ID if you must but I'd say it's a load of pain that you just don't want to get into for something this simple.

提交回复
热议问题