Composite Key with Hibernate

后端 未结 4 738
死守一世寂寞
死守一世寂寞 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:14

    I add to the answer of Chris Winters, but using foreign keys in order to implement it. You can write:

    <composite-id>
        <key-many-to-one name="username" class="com.foo.User" />
        <key-many-to-one name="role_id" class="com.foo.Role" />
    </composite-id>
    

    This will do the job and also take care of foreign keys, and will give you the flexibility to define by yourself which tables are created in the database by Hibernate.

    0 讨论(0)
  • 2021-02-15 07:14

    Look in the hibernate docs for the 'composite-id' element. The following may at least give you the intent -- replace your id element with:

    <composite-id>
        <key-property name="username"/>
        <key-property name="role"/>
    </composite-id>
    

    Note that it's strongly recommended to instead make your ID a separate class ('component') that implements hashCode and equals properly, and that is Serializable. Otherwise you'll have only very awkward ways to lookup your object using session.get() or session.load().

    0 讨论(0)
  • 2021-02-15 07:32

    Maybe in fact something like this:

    <class name="User" table="users">
      <id name="username" column="user_name"/>
      <property name="password" column="user_pass" not-null="true"/>
      <set name="roles" table="user_roles">
        <key column="user_name"/>
        <element type="string" column="role_name" />
      </set>
    </class>
    

    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:

    <class name="User" table="users">
      <id name="username" column="user_name"/>
      <property name="password" column="user_pass" not-null="true"/>
      <set name="roles" table="user_roles">
        <key column="user_name"/>
        <many-to-many column="role_name" class="Role"/>
      </set>
    </class>
    <class name="Role" table="roles">
      <id name="name" column="role_name"/>
    </class>
    

    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.

    0 讨论(0)
  • 2021-02-15 07:34

    Sergio, look at this composite key mapping in hibernate tutorial. Might help you out.

    0 讨论(0)
提交回复
热议问题