2

I'm using @Embeddable REmbeddedReference twice in my entity RTask. This embeddable contains QName property which type is defined as org.hibernate.usertype.UserType.

@Embeddable
public class REmbeddedReference implements Serializable {

    private String targetOid;
    private String description;
    private String filter;
    private RContainerType type;
    private QName relation;

    @Columns(columns = {
            @Column(name = "relation_namespace"),
            @Column(name = "relation_localPart")
    })
    public QName getRelation() {        return relation;    }

    @Column(length = 36, insertable = true, updatable = true, nullable = true)
    public String getTargetOid() {        return targetOid;    }

    @Type(type = "org.hibernate.type.TextType")
    public String getDescription() {        return description;    }

    @Enumerated(EnumType.ORDINAL)
    public RContainerType getType() {        return type;    }

    @Type(type = "org.hibernate.type.TextType")
    public String getFilter() {        return filter;    }

    ...setters...
}

RTask looks like this:

@Entity
@ForeignKey(name = "fk_task")
public class RTask extends RObject {
    private REmbeddedReference objectRef;
    private REmbeddedReference ownerRef;
    ...other fields...

    @Embedded
    public REmbeddedReference getObjectRef() {
        return objectRef;
    }

    @Embedded
    public REmbeddedReference getOwnerRef() {
        return ownerRef;
    }

    ...other methods...
}

During startup I get: Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.evolveum.midpoint.repo.sql.data.common.RTask column: relation_namespace (should be mapped with insert="false" update="false")

I tried to update my NamingStrategy, method logicalColumnName which generated objectRef_relation_namespace and ownerRef_relation_namespace. But then I get: Caused by: org.hibernate.DuplicateMappingException: Table [m_task] contains phyical column name [relation_namespace] represented by different logical column names: [objectRef_relation_namespace], [ownerRef_relation_namespace]

Do you have any idea how to map property relation in REmbeddedReference properly?

viliam
  • 503
  • 6
  • 23
  • Get an answer here http://stackoverflow.com/questions/331744/jpa-multiple-embedded-fields – d1e Jul 02 '12 at 08:10
  • I know about `DefaultComponentSafeNamingStrategy` but when I remove `@Columns` from `REmbeddedReference` then I get: `Caused by: org.hibernate.MappingException: property mapping has wrong number of columns: com.evolveum.midpoint.repo.sql.data.common.RTask.objectRef type: component[description,filter,relation,targetOid,type]` because that QName (custom user type) field `relation` has to be represented with two columns... – viliam Jul 02 '12 at 08:45
  • Your embeddable has column definitions. Obviously, you can't have two columns with the same name in a table. Look at the second post in the link provided. You should be able to use @AttributeOverride to name the columns appropriately. – jeff Jul 02 '12 at 13:18

1 Answers1

2

Solution:

@AttributeOverride(name="relation.namespace", column=@Column(name="object_namespace")
@AttributeOverride(name="relation.localPart", column=@Column(name="object_localpart")
@Embedded
public REmbeddedReference getObjectRef() {
    return objectRef;
}

@AttributeOverride(name="relation.namespace", column=@Column(name="owner_namespace")
@AttributeOverride(name="relation.localPart", column=@Column(name="owner_localpart")
@Embedded
public REmbeddedReference getOwnerRef() {
    return ownerRef;
}

didn't work. I had to change QName type implementation from CompositeUserType to another embeddable entity RQName which represents this property in REmbeddedReference embeddable.

viliam
  • 503
  • 6
  • 23