Hello!
Hibernate version: 3.1.2, annotations: 3.1beta8, jdk: 1.5.0_06
It seems that delivering collections is broken while there must have been used a discriminator in single-table-per-hierarchy case.
I wrote a little example showed the problem. Please help - whether it is a hibernate or mine problem. So, classes (I omit imports and getters-setters):
@Entity
@Table(name = "ITEMS_DRAW")
public class ItemsDraw {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id")
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "ParentDrawId")
private Set<ItemA> as;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "ParentDrawId")
private Set<ItemB> bs;
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ItemType", discriminatorType = DiscriminatorType.STRING)
@Table(name = "ITEMS")
public abstract class AbstractItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id")
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "ParentDrawId")
private ItemsDraw draw;
}
@Entity
@DiscriminatorValue("A")
public class ItemA extends AbstractItem {
@Column(name = "DateField", nullable = true)
private Date date;
}
@Entity
@DiscriminatorValue("B")
public class ItemB extends AbstractItem {
@Column(name = "NumberField", nullable = true)
private Integer number;
}
MySQL is used. The schema generated is:
create table ITEMS
(ItemType varchar(10) not null,
Id bigint not null auto_increment,
DateField datetime,
NumberField integer,
ParentDrawId bigint not null,
primary key (Id));
create table ITEMS_DRAW
(Id bigint not null auto_increment,
primary key (Id));
alter table ITEMS
add index FK42BEFA0AD2BA03E (ParentDrawId),
add constraint FK42BEFA0AD2BA03E
foreign key (ParentDrawId) references ITEMS_DRAW (Id);
I create 1 ItemsDraw and 2 items inside, one A and one B (omit session creation):
ItemsDraw d = new ItemsDraw();
session.save(d);
ItemA a = new ItemA();
a.setDate(new Date());
a.setDraw(d);
session.save(a);
ItemB b = new ItemB();
b.setNumber(100);
b.setDraw(d);
session.save(b);
Then, I try to deliver ItemsDraw object (full main function follows):
public class Test {
public static void main(String args[]) throws Exception {
AnnotationConfiguration acfg = Main.configEntityMappings();
Configuration cfg = acfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Criteria c = session.createCriteria(ItemsDraw.class);
List cList = c.list();
System.out.println(cList);
Set aSet = ((ItemsDraw) cList.get(0)).getAs();
System.out.println(aSet);
session.close();
}
public static AnnotationConfiguration configEntityMappings() {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(ItemsDraw.class);
cfg.addAnnotatedClass(AbstractItem.class);
cfg.addAnnotatedClass(ItemA.class);
cfg.addAnnotatedClass(ItemB.class);
return cfg;
}
}
The result is:
[hib.scene.ItemsDraw@1]
[hib.scene.ItemA@2, hib.scene.ItemA@1]
which is the evidence that no discriminator is used while delivering a collection by getAs(); (hib.scene is a package for the example). I did some debug and found out that OneToManyPersister and AbstractCollectionPersister both seem to know nothing about discriminators at all!
Is it a bug or not? If not, how to make it work? Please, help. I suppose there is something to do with magic "discriminator force=true" statement, but I could not find its annotation variant to test...
Thank you in advance.
_________________ Sincerely yours,
Serg
|