-->
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.  [ 5 posts ] 
Author Message
 Post subject: problem mapping a List
PostPosted: Tue Apr 07, 2009 12:56 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Using Hibernate 3.2 and JPA Annotations, I'm able to map a Set but getting the following exception when I change the same mapping to a List (and adding an @OrderBy).

Code:
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.xxx.RelationTarget.target


Here's the OneToMany

Code:
    @OneToMany(targetEntity = com.xxx.RelationTarget.class, mappedBy = "relation", cascade = {CascadeType.REMOVE,
            CascadeType.PERSIST })
    @OrderBy("sequence")
    @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<RelationTarget> getTargets() {
        return _relationTargets;
    }


Here's the ManyToOne (that the exception is complaining about)

Code:
    @ManyToOne(targetEntity = com.xxx.Relation.class, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "relationid", insertable = false, updatable = false)
    public Relation getRelation() {
        return _id.getRelation();
    }


Insertable and Updatable are set to false because this class uses an @Embeddable id.

Code:
    @Id
    protected RelationTargetPK getId() {
        return _id;
    }


Here's the @embeddable PK class mapping

Code:
    @ManyToOne(targetEntity = com.hli.redhat.value.Relation.class, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "relationid")
    public Relation getRelation() {
        return _relation;
    }


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 12:39 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
This code is not enough. Please post full code of both entity classes and the primary key class.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 10:32 am 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Thanks for taking a look into this for me. Here's the Relation Class:

Code:
@Entity
@SecondaryTable(name = "Relations")
@DiscriminatorValue(value = ValueTypeIds.RELATION)
public class Relation extends ELObject {

    private Concept _source;
    private List<RelationTarget> _relationTargets = new ArrayList<RelationTarget>();
    private RelationType _relationType;

    @Transient
    @Override
    public RelationId getId() {
        return (RelationId) super.getId();
    }

    @ManyToOne(targetEntity = com.xxx.Concept.class)
    @JoinColumn(name = "sourceid", table = "Relations")
    public Concept getSource() {
        return _source;
    }

    @OneToMany(targetEntity = com.xxx.RelationTarget.class, mappedBy = "relation", cascade = {CascadeType.REMOVE,
            CascadeType.PERSIST })
    @OrderBy("sequence")
    @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<RelationTarget> getTargets() {
        return _relationTargets;
    }

    protected List<RelationTarget> setTargets(List<RelationTarget> targets) {
        _relationTargets = targets;
        return _relationTargets;
    }

    protected Concept setSource(Concept source) {
        _source = source;
        return _source;
    }

    @ManyToOne(targetEntity = com.xxx.RelationType.class)
    @JoinColumn(name = "relationdefinitionid", table = "Relations")
    public RelationType getRelationType() {
        return _relationType;
    }

    protected RelationType setRelationType(RelationType relationType) {
        _relationType = relationType;
        return _relationType;
    }

}


Here's the other entity class, RelationTarget:

Code:
@Entity
@Table(name = "RelationTargets")
public class RelationTarget extends ValueObject {

    private RelationTargetPK _id = new RelationTargetPK();
    private Concept _target;

    @Id
    protected RelationTargetPK getId() {
        return _id;
    }

    protected void setId(RelationTargetPK id) {
        _id = id;
    }

    @ManyToOne(targetEntity = com.xxx.Relation.class, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "relationid", insertable = false, updatable = false)
    public Relation getRelation() {
        return _id.getRelation();
    }

    private void setRelation(Relation relation) {
    }

    @ManyToOne(targetEntity = com.xxx.Concept.class, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "targetid")
    public Concept getTarget() {
        return _target;
    }

    public Concept setTarget(Concept target) {
        _target = target;
        return _target;
    }

    @Column(name = "sequence", insertable = false, updatable = false)
    public int getSequence() {
        return _id.getSequence();
    }

    protected void setSequence(int sequence) throws IllegalArgumentException {
        _id.setSequence(sequence);
    }
}


And here's the PK class:

Code:
@Embeddable
public class RelationTargetPK implements Serializable {

    private Relation _relation;
    private int _sequence;

    @ManyToOne(targetEntity = com.xxx.Relation.class, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "relationid")
    public Relation getRelation() {
        return _relation;
    }

    public Relation setRelation(Relation relation) {
        _relation = relation;
        return _relation;
    }

    @Column(name = "sequence")
    public int getSequence() {
        return _sequence;
    }

    public void setSequence(int sequence) {
        _sequence = sequence;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((_relation == null) ? 0 : _relation.hashCode());
        result = prime * result + _sequence;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        RelationTargetPK other = (RelationTargetPK) obj;
        if (_relation == null) {
            if (other._relation != null) {
                return false;
            }
        } else if (!_relation.equals(other._relation)) {
            return false;
        }
        if (_sequence != other._sequence) {
            return false;
        }
        return true;
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 12:37 pm 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Hi, I have modified 2 relation classes (Relation,RelationTarget). Please try with these changes.

Modification in Relation class

Code:
@OrderBy("id.sequence") //set property name in order by clause


Relation Target

Code:
@Entity
@Table(name = "RelationTargets")
public class RelationTarget extends ValueObject {

   private RelationTargetPK _id = new RelationTargetPK();
   private Relation _relation; // ---------------->>>>adding new filed
   private Concept _target;

   @Id
   protected RelationTargetPK getId() {
      return _id;
   }

   protected void setId(RelationTargetPK id) {
      _relation = id.getRelation(); // setting _relation value
      _id = id;
   }

   @ManyToOne(targetEntity = com.xxx.Relation.class, cascade = CascadeType.PERSIST)
   @JoinColumn(name = "relationid", insertable = false, updatable = false)
   public Relation getRelation() {
      return _relation; // ------------------>>>Modified
   }

   private void setRelation(Relation relation) {
   }

   @ManyToOne(targetEntity = com.xxx.Concept.class, cascade = CascadeType.PERSIST)
   @JoinColumn(name = "targetid")
   public Concept getTarget() {
      return _target;
   }

   public Concept setTarget(Concept target) {
      _target = target;
      return _target;
   }

   @Column(name = "sequence", insertable = false, updatable = false)
   public int getSequence() {
      return _id.getSequence();
   }

   protected void setSequence(int sequence) throws IllegalArgumentException {
      _id.setSequence(sequence);
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 12:35 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Thanks, Parmendratyagi -

However, that did not change anything. In fact, I have another scenario with a PK class and that one works fine without the "Many" entity having an object reference to the "One" class (i.e. you added "relation" to RelationTarget class).

I tried a simple mapping of a List on an entity that does not have a composite key and it worked fine. So this seems to be having a List mapped in conjunction with the composite key / PK class.

Any other ideas?


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