English 中文(简体)
@IdClass with non primative @Id
原标题:

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes.

class User {
    private long id;
    ...
}

class Token {
    private User user;
    private String series;
    ...
}

I m using the orm.xml to map the classes because they re actually part of a higher level API that I don t want to depend on JPA - it has a number of implementations.

Here it is:

...
<entity class="User">
    <attributes>
        <id name="id">
            <generated-value strategy="AUTO"/>
        </id>
        ...
    </attributes>
</entity>

<entity class="Token">
    <id-class class="TokenPK"/>
    <attributes>
        <id name="series"/>
        <id name="user"/>
        <many-to-one name="user"/>
    </attributes>
</entity>

Finally to make it all work, I ve created the TokenPK class and it looks like this:

public class TokenPK implements Serializable {

    private String series;
    private User user;

    public TokenPK() {
    }

    public TokenPK(String series, User user) {
        this.series = series;
        this.user = user;
    }

    public String getSeries() {
        return series;
    }

    public void setSeries(String series) {
        this.series = series;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RememberMeTokenPK that = (TokenPK) o;

        if (!series.equals(that.series)) return false;
        if (!user.equals(that.user)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = series.hashCode();
        result = 31 * result + user.hashCode();
        return result;
    }
}

The problem I m having is that Hibernate is complaning that it can t create the mysql tables because BLOB/TEXT column user used in key specification without a key length .

My issue is actually that the columns are being stored as BLOBs in the first place. Until I put the id-class in it was working just fine, user was linked via it s id. How can I make Hibernate use the long value for the user s id it was using as the primary key?

Updated orm.xml:

<entity class="Token">
    <id-class class="TokenPK"/>
    <attributes>
        <id name="series"/>
        <id name="user">
            <column name="userId"/>
        </id>
        <many-to-one name="user">
            <join-column name="userId" insertable="false" updatable="false"/>
        </many-to-one>
    </attributes>
</entity>
最佳回答

Define the composite key with String series and int userId, and specify a join-column id for the User in Token. I think you will also have to add insertable="false", updatable="false".

 <composite-id name="TikenPK" class="yourpackage.TokenPK"> 
        <key-property name="series" column="series" type="string" />
        <key-property name="userId" column="userId" type="integer"/>
       </composite-id> 
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签