[
https://hibernate.onjira.com/browse/HHH-6662?page=com.atlassian.jira.plug...
]
Patrick Heinzelmann edited comment on HHH-6662 at 4/26/12 8:33 PM:
-------------------------------------------------------------------
I have two cases, where I'm using @AssociationOverride and in both cases Hibernate
isn't working as expected. I'm using Hibernate 3.6.10.Final
Case 1:
@MappedSuperclass
public class BaseObject extends AbstractAuditableEntity implements Serializable{
@ManyToMany
@JoinTable(name = "PRO_BaseObject_Tag", joinColumns = @JoinColumn(name =
"baseObjectId"), inverseJoinColumns = @JoinColumn(name = "tagId"))
private List<Tag> tags;
}
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildObject1")
@AssociationOverride(name="tags", joinTable=@JoinTable(name =
"PRO_ChildObject1_Tag", joinColumns = @JoinColumn(name =
"childObject1Id"), inverseJoinColumns = @JoinColumn(name = "tagId")))
public class ChildObject1 extends BaseObject implements Serializable
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildObject2")
@AssociationOverride(name="tags", joinTable=@JoinTable(name =
"PRO_ChildObject2_Tag", joinColumns = @JoinColumn(name =
"childObject2Id"), inverseJoinColumns = @JoinColumn(name = "tagId")))
public class ChildObject2 extends BaseObject implements Serializable
-------------------------------------------------------------
=> Hibernate created three tables.
Table 1: PRO_BaseObject_Tag with 2 columns (baseObjectId and tagId), but two FK are
pointing to baseObjectId. From my point of view this table shouldn't exist!
CREATE TABLE `pro_baseobject_tag` (
`baseObjectId` varchar(32) NOT NULL,
`tagId` varchar(32) NOT NULL,
KEY `FK5818CF425B717484` (`tagId`),
KEY `FK5818CF42218D7166` (`baseObjectId`),
KEY `FK5818CF42938FAEA4` (`baseObjectId`),
CONSTRAINT `FK5818CF42218D7166` FOREIGN KEY (`baseObjectId`) REFERENCES
`PRO_ChildObject1` (`id`),
CONSTRAINT `FK5818CF425B717484` FOREIGN KEY (`tagId`) REFERENCES `PRO_Tag` (`id`),
CONSTRAINT `FK5818CF42938FAEA4` FOREIGN KEY (`baseObjectId`) REFERENCES
`PRO_ChildObject2` (`id`)
Table 2: PRO_ChildObject1_PRO_Tag, which are using the wrong name for the table
Table 3: PRO_ChildObject2_PRO_Tag, which are using the wrong name for the table
Case 2: As the default association of @OneToMany is JoinTable, I didn't annotated the
aImages and bImages with @JoinTable as the associations were overwritten by the inherited
classes.
@MappedSuperclass
public abstract class BaseInfo implements Serializable{
@OneToMany
private List<ImageFile> aImages;
@OneToMany
private List<ImageFile> bImages;
}
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildInfo1")
@AssociationOverrides({
@AssociationOverride(name="aImages", joinTable=@JoinTable(name =
"PRO_ChildInfo1_AImageInfo", joinColumns = @JoinColumn(name =
"childInfo1Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId"))),
@AssociationOverride(name="bImages", joinTable=@JoinTable(name =
"PRO_ChildInfo1_BImageInfo", joinColumns = @JoinColumn(name =
"childInfo1Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId")))
})
public class ChildInfo1 extends BaseInfo implements Serializable
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildInfo2")
@AssociationOverrides({
@AssociationOverride(name="aImages", joinTable=@JoinTable(name =
"PRO_ChildInfo2_AImageInfo", joinColumns = @JoinColumn(name =
"childInfo2Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId"))),
@AssociationOverride(name="bImages", joinTable=@JoinTable(name =
"PRO_ChildInfo2_BImageInfo", joinColumns = @JoinColumn(name =
"childInfo2Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId")))
})
public class ChildInfo2 extends BaseInfo implements Serializable
-------------------------------------------------------------
Instead of creating 4 tables (PRO_ChildInfo1_AImageFile, PRO_ChildInfo2_AImageFile,
PRO_ChildInfo1_BImageFile, PRO_ChildInfo2_BImageFile ), Hibernate created only 2 tables
(PRO_ChildInfo1_PRO_ImageFile, PRO_ChildInfo2_PRO_ImageFile) in this case. Here is the
exported SQL CREATE statement of one table:
CREATE TABLE `pro_childinfo1_pro_imagefile` (
`PRO_BaseInfo_id` varchar(32) NOT NULL,
`bImages_id` varchar(32) NOT NULL,
`aImages_id` varchar(32) NOT NULL,
UNIQUE KEY `bImages_id` (`bImages_id`),
UNIQUE KEY `aImages_id` (`aImages_id`),
KEY `FKC8A1650061BCF17B` (`aImages_id`),
KEY `FKC8A165004F795D9A` (`bImages_id`),
KEY `FKC8A165003CCEA0AD` (`PRO_BaseInfo_id`),
CONSTRAINT `FKC8A165003CCEA0AD` FOREIGN KEY (`PRO_BaseInfo_id`) REFERENCES
`PRO_BaseInfo` (`id`),
CONSTRAINT `FKC8A165004F795D9A` FOREIGN KEY (`bImages_id`) REFERENCES `PRO_ImageFile`
(`id`),
CONSTRAINT `FKC8A1650061BCF17B` FOREIGN KEY (`aImages_id`) REFERENCES `PRO_ImageFile`
(`id`)
In the second case, Hibernate is completely ignoring any overwritten associations.
Note: The Hibernate Github trunk (4.x) still contains same code as described by Richard
above !!!!
was (Author: patrickheinzelmann):
I have two cases, where I'm using @AssociationOverride and in both cases Hibernate
isn't working as expected. I'm using Hibernate 3.6.10.Final
Case 1:
@MappedSuperclass
public class BaseObject extends AbstractAuditableEntity implements Serializable{
@ManyToMany
@JoinTable(name = "PRO_BaseObject_Tag", joinColumns = @JoinColumn(name =
"baseObjectId"), inverseJoinColumns = @JoinColumn(name = "tagId"))
private List<Tag> tags;
}
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildObject1")
@AssociationOverride(name="tags", joinTable=@JoinTable(name =
"PRO_ChildObject1_Tag", joinColumns = @JoinColumn(name =
"childObject1Id"), inverseJoinColumns = @JoinColumn(name = "tagId")))
public class ChildObject1 extends BaseObject implements Serializable
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildObject2")
@AssociationOverride(name="tags", joinTable=@JoinTable(name =
"PRO_ChildObject2_Tag", joinColumns = @JoinColumn(name =
"childObject2Id"), inverseJoinColumns = @JoinColumn(name = "tagId")))
public class ChildObject2 extends BaseObject implements Serializable
-------------------------------------------------------------
=> Hibernate created three tables.
Table 1: PRO_BaseObject_Tag with 2 columns (baseObjectId and tagId), but two FK are
pointing to baseObjectId. From my point of view this table shouldn't exist!
CREATE TABLE `pro_baseobject_tag` (
`baseObjectId` varchar(32) NOT NULL,
`tagId` varchar(32) NOT NULL,
KEY `FK5818CF425B717484` (`tagId`),
KEY `FK5818CF42218D7166` (`baseObjectId`),
KEY `FK5818CF42938FAEA4` (`baseObjectId`),
CONSTRAINT `FK5818CF42218D7166` FOREIGN KEY (`baseObjectId`) REFERENCES
`PRO_ChildObject1` (`id`),
CONSTRAINT `FK5818CF425B717484` FOREIGN KEY (`tagId`) REFERENCES `PRO_Tag` (`id`),
CONSTRAINT `FK5818CF42938FAEA4` FOREIGN KEY (`baseObjectId`) REFERENCES
`PRO_ChildObject2` (`id`)
Table 2: PRO_ChildObject1_PRO_Tag, which are using the wrong name for the table
Table 3: PRO_ChildObject2_PRO_Tag, which are using the wrong name for the table
Case 2: As the default association of @OneToMany is JoinTable, I didn't annotated the
aImages and bImages with @JoinTable as the associations were overwritten by the inherited
classes.
@MappedSuperclass
public abstract class BaseInfo implements Serializable{
@OneToMany
private List<ImageFile> aImages;
@OneToMany
private List<ImageFile> bImages;
}
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildInfo1")
@AssociationOverrides({
@AssociationOverride(name="aImages", joinTable=@JoinTable(name =
"PRO_ChildInfo1_AImageInfo", joinColumns = @JoinColumn(name =
"childInfo1Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId"))),
@AssociationOverride(name="bImages", joinTable=@JoinTable(name =
"PRO_ChildInfo1_BImageInfo", joinColumns = @JoinColumn(name =
"childInfo1Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId")))
})
public class ChildInfo1 extends BaseInfo implements Serializable
-------------------------------------------------------------
@Entity
@Table(name = "PRO_ChildInfo2")
@AssociationOverrides({
@AssociationOverride(name="aImages", joinTable=@JoinTable(name =
"PRO_ChildInfo2_AImageInfo", joinColumns = @JoinColumn(name =
"childInfo2Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId"))),
@AssociationOverride(name="bImages", joinTable=@JoinTable(name =
"PRO_ChildInfo2_BImageInfo", joinColumns = @JoinColumn(name =
"childInfo2Id"), inverseJoinColumns = @JoinColumn(name =
"imageFileId")))
})
public class ChildInfo2 extends BaseInfo implements Serializable
-------------------------------------------------------------
Instead of creating 4 tables (PRO_ChildInfo1_AImageFile, PRO_ChildInfo2_AImageFile,
PRO_ChildInfo1_BImageFile, PRO_ChildInfo2_BImageFile ), Hibernate created only 2 tables
(PRO_ChildInfo1_PRO_ImageFile, PRO_ChildInfo2_PRO_ImageFile) in this case. Here is the
exported SQL CREATE statement of one table:
CREATE TABLE `pro_childinfo1_pro_imagefile` (
`PRO_BaseInfo_id` varchar(32) NOT NULL,
`bImages_id` varchar(32) NOT NULL,
`aImages_id` varchar(32) NOT NULL,
UNIQUE KEY `bImages_id` (`bImages_id`),
UNIQUE KEY `aImages_id` (`aImages_id`),
KEY `FKC8A1650061BCF17B` (`aImages_id`),
KEY `FKC8A165004F795D9A` (`bImages_id`),
KEY `FKC8A165003CCEA0AD` (`PRO_BaseInfo_id`),
CONSTRAINT `FKC8A165003CCEA0AD` FOREIGN KEY (`PRO_BaseInfo_id`) REFERENCES
`PRO_BaseInfo` (`id`),
CONSTRAINT `FKC8A165004F795D9A` FOREIGN KEY (`bImages_id`) REFERENCES `PRO_ImageFile`
(`id`),
CONSTRAINT `FKC8A1650061BCF17B` FOREIGN KEY (`aImages_id`) REFERENCES `PRO_ImageFile`
(`id`)
In the second case, Hibernate is completely ignoring any overwritten associations.
Note: The Hibernate Github trunk (4.x) still contains same code as decribed by Richard
above !!!!
@AssociationOverride with @JoinTable does not work
--------------------------------------------------
Key: HHH-6662
URL:
https://hibernate.onjira.com/browse/HHH-6662
Project: Hibernate ORM
Issue Type: Bug
Components: core
Affects Versions: 3.6.7
Environment: Hibernate 3.6.7.Final
Reporter: Richard Kohl
Priority: Critical
Labels: associationOverride
Attachments: test-case.zip
Original Estimate: 1h
Remaining Estimate: 1h
@AssociationOverride is completely ignored when used with @JoinTable (test case encloded,
but it will probably happen always).
I looked through the Hibernate code and that reason for that is probably the typo in
org.hibernate.cfg.AbstractPropertyHolder.buildHierarchyColumnOverride(XClass), particulary
lines:
currentOverride.putAll( columnOverride ); //subclasses have precedence over superclasses
currentJoinOverride.putAll( joinColumnOverride ); //subclasses have precedence over
superclasses
currentJoinOverride.putAll( joinColumnOverride ); //subclasses have precedence over
superclasses
Instead, the should be:
currentOverride.putAll( columnOverride ); //subclasses have precedence over superclasses
currentJoinOverride.putAll( joinColumnOverride ); //subclasses have precedence over
superclasses
currentJoinTableOverride.putAll( joinTableOverride); //subclasses have precedence over
superclasses
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira