-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unidirectional One-to-Many with a Collection of Abstract
PostPosted: Tue Mar 15, 2016 10:55 am 
Newbie

Joined: Tue Mar 15, 2016 10:42 am
Posts: 2
I'm having an issue with JPA. Basically, what I have is an entity with a list of an abstract type and I need each element of the list to be persisted in its corresponding table with a foreign key to relate to the entity container. Here is the code:

Code:

@Entity(name = "container")
public class Container{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private BigInteger id;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="container_id", referencedColumnName="id")
private List<AbstractType> types;
}


Abstract type:

Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractType{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private BigInteger id;

private String someProp;

@Column(name="container_id")
private BigInteger containerId;
}


Type A:
Code:
@Entity(name = "type_a")
@Transactional
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class TypeA extends AbstractType{
private String prop1;
private String prop2;
}


Type B:

Code:
@Entity(name = "type_b")
@Transactional
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class TypeB extends AbstractType{
private String prop3;
private String prop4;
}


I'm having a SQL error. The generated query tries to update the table of the abstract type (which shouldnt exist). This is part of the query:

Code:
update hibernate_sequences set sequence_next_hi_value = ? where
sequence_next_hi_value = ? and sequence_name = 'abstract_type'
insert into type_a (some_prop, entity_id, prop1, prop2) values (?, ?, ?, ?)
insert into type_b (some_prop, entity_id, prop3, prop4) values (?, ?, ?, ?)
update abstract_type set entity_id=? where id=?


As you can see, it's trying to update a table which doesn't (and shouldnt) exist. 'abstract_type' table.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Unidirectional One-to-Many with a Collection of Abstract
PostPosted: Wed Mar 16, 2016 6:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try removing @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) from the subclasses.


Top
 Profile  
 
 Post subject: Re: Unidirectional One-to-Many with a Collection of Abstract
PostPosted: Wed Mar 16, 2016 9:22 am 
Newbie

Joined: Tue Mar 15, 2016 10:42 am
Posts: 2
Thanks for the answer. Already tried, didn't work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.