Surfing the net to understand how to map with jpa a concrete class
extending an abstract one and implementing an interface we saw on this
link
[url]
http://www.hibernate.org/hib_docs/annot ... ntity.html
[/url]
that it is not possible , but we achieved to do that in a tricky way shown below :
We would like to know if it could be a solution , and if there are any other annotations with jpa which simplifies much our development
greetings...
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public interface Animale
{
@Id
public Integer getId();
public void setId(Integer id);
public void cammina();
}
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Bestiola {
@Id@GeneratedValue(strategy=GenerationType.TABLE)
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
Code:
@Entity
public class Cane extends Bestiola implements Animale{
@OneToMany
private Collection<Animale> l = null;
public Collection<Animale> getL() {
return l;
}
public void setL(Collection<Animale> l) {
this.l = l;
}
public void cammina() {
}
public Integer getId() {
return super.getId();
}
public void setId(Integer id) {
super.setId(id);
}
}
Code:
@Entity
public class Gatto extends Bestiola implements Animale{
public void cammina() {
}
public Integer getId() {
return super.getId();
}
public void setId(Integer id) {
super.setId(id);
}
}
Code:
public class TestCane extends TestCase{
private EntityManagerFactory emf = null;
private EntityManager em = null;
private EntityTransaction et = null;
public void setUp(){
emf = Persistence.createEntityManagerFactory("Test");
em = emf.createEntityManager();
}
public void testA(){
et = em.getTransaction();
et.begin();
Cane c = new Cane();
Cane c2 = new Cane();
Gatto g = new Gatto();
Collection coll = new ArrayList<Animale>();
coll.add(c2);
coll.add(g);
c.setL(coll);
em.persist(g);
em.persist(c2);
em.persist(c);
et.commit();
}
public void tearDown(){
em.close();
emf.close();
}
}