Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

mappedSuperClass

843833Feb 25 2008 — edited Feb 25 2008
Hi *,

I'm having some trouble with a MappedSuperClass I defined. Let me build up...

Node, which keeps a reference to a Reading object:
@Entity
public class Node implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private Long id;
    
    @EmbeddedId
    private NodePK nodePK;

    @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
    private Network network;
    
    @OneToOne
    private Reading latestReading;
    
    ...//(constructors and methods)
}
Unit, which is the superclass of Reading:
@MappedSuperclass
public abstract class Unit implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @OneToOne
    private Node node;
    
    public Unit() {}
    
    ...//(constructors and methods)
}
Reading, which inherits from Unit
@Entity
public class Reading extends Unit implements Serializable {
    
    //private Node node;                         <- PROBLEM

    public Reading() {}
    
    ...//(constructor and methods)

}
So, I create a Node object which stores its latest Reading-object, which is a subclass of Unit. To make Unit a MappedSuperClass, I annotated the class accordingly and extend its subclass Reading.

I further have a stateless session bean which does the following:
...
Node n1 = new Node(1, net1);
em.persist(n1);

Reading reading1 = new Reading(n1, null, 1, 2, 3);
em.persist(reading1);
n1.setLatestReading(reading1);

removeNode(1);

...
private void removeNode(int address) {
        Node node = (Node) em.createQuery("SELECT x " +
                "FROM Node x " +
                "WHERE x.nodePK.nodeAddress = :addr").setParameter("addr", address).getSingleResult();

        em.remove(node);
}
So basically just creating a node, creating a reading, set this reading as the nodes latest reading and finally remove this all from persistence. And there it goes wrong! When running my app I get:
Caused by: org.apache.derby.client.am.SqlException: DELETE on table 'NODE' caused a violation of foreign key constraint 'READINGNODEADDRESS' for key (1,321).  The statement has been rolled back.
...
I tried a bunch off stuff including adding AssociationOverride statements and moving the node property from the superclass to its subclasses. The latter works, but is kind of foolish because then the superclass has no other significant properties and I could as well not use a superclass (FYI, Reading will not be the only class extending Unit).

Something that also seems to work, is adding the node attribute both to the superclass and the subclass (so uncommenting the problem line I indicated above).

Can someone help me with this? I've been struggling for days with this and about to give up so...

Thanks!
Vaak
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2008
Added on Feb 25 2008
0 comments
137 views