-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: combined key which incl another entity: column size too long
PostPosted: Fri Nov 22, 2013 8:17 am 
Newbie

Joined: Thu Nov 21, 2013 8:29 am
Posts: 2
Environment:
java 1.7.0_25 (64 bit)
jboss 7.1.1.Final
Windows 7
db: db2 9.7

hibernate 4.2.7.SP1
jpa-2.0-api


Hi,
I'm fighting with a combined primary key, which includes an entity reference and a simple value.

Hibernate generates the primary key correctly, but the column size of the column, which references the entity, is 255 instead of 36 (which is the size of the referenced column).

This is what my code looks like:

Note: I use a UUID as primary key within my other entities. The column length for those columns is always 36.

Code:
import static javax.persistence.FetchType.LAZY;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;


@MappedSuperclass
@IdClass(AcePK.class)
public abstract class Ace {

    protected FilterRule filterRule;

    @ManyToOne(cascade = {}, fetch = LAZY, targetEntity = FilterRule.class)
    @JoinColumn(name = "RULE_UUID", referencedColumnName="UUID",
                updatable = false, nullable = false )
    @Id
    public FilterRule getFilterRule() {
        return this.filterRule;
    }

    public boolean setFilterRule(FilterRule rule) {
        this.filterRule = rule;
    }


    private String grantedTo;

    @Column(name = GRANTED_TO, nullable = false, length = 36)
    @Id
    public String getGrantedTo() {
        return this.grantedTo;
    }

    public void setGantedTo(String value) {
        this.grantedTo = value;
    }
}



The class which defines the combined primary key:
Code:
import java.io.Serializable;

import javax.persistence.Column;


public class AcecPK implements Serializable {

    private static final long serialVersionUID = -4837448484343854819L;

    public String filterRule;
    public String grantedTo;

    @Column(name = "RULE_UUID", length = 36) //I've tried with and without including this annotation; the result within the db is always the same
    public String getFilterRule() {
        return filterRule;
    }

    public void setFilterRule(String rule) {
        this.filterRule = rule;
    }


    public String getGrantedTo() {
        return grantedTo;
    }

    public void setGrantedTo(String granted) {
        this.grantedTo = granted;
    }

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

        AcePK acePK = (AcePK) o;

        if (filterRule != null ? !filterRule.equals(acePK.filterRule) : acePK.filterRule != null) {
            return false;
        }
        if (grantedTo != null ? !grantedTo.equals(acePK.grantedTo) : acePK.grantedTo != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = filterRule != null ? filterRule.hashCode() : 0;
        result = 31 * result + (grantedTo != null ? grantedTo.hashCode() : 0);
        return result;
    }
}



The generated columns within my database look like:

    COLUMN_NAME, COLUMN_SIZE, TYPE_NAME, ORDINAL_POSITION
    RULE_UUID, 255, VARCHAR, 1
    GRANTED_TO, 36, VARCHAR, 2


How can I get rid of this 255 characters?

Thanks for any help.
Camilla


Top
 Profile  
 
 Post subject: Re: combined key which incl another entity: column size too long
PostPosted: Fri Nov 22, 2013 10:32 am 
Newbie

Joined: Thu Nov 21, 2013 8:29 am
Posts: 2
I found, that removing
@IdClass(AcePK.class)
from my Ace class resolves the problem with the column length.

But now, hibernate changes the ordinals within the generated index.

If I use the @IdClass, the ordinals are
RULE_UUID = 1
GRANTED_TO = 2
which is the way I want it to be.

But if I skip the @IdClass-Annotation, hibernate generates the index with
RULE_UUID = 2
GRANTED_TO = 1

I can't specify the ordinal within the @Id annotation.
Is there another annotation to specify the ordinal of the column within my unique composite index?

Cheers,
Camilla


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.