-->
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.  [ 2 posts ] 
Author Message
 Post subject: ManyToOne from inside EmbeddedId - code attached
PostPosted: Thu Sep 03, 2009 10:52 am 
Newbie

Joined: Sun Aug 30, 2009 11:04 am
Posts: 4
Hey folks,
I have three classes (in a ticket system)
Ticket.java, Recipient.java, RecipientPK.java

RecipientPK is the embeddedId in Recipient, and the point is that every Ticket can have many Recipients.

RecipientPK.java

Code:
@SuppressWarnings("serial")
@org.hibernate.annotations.AccessType("property")
@Embeddable
public class RecipientPK implements Serializable {

   private long ticketId;
   private long userId;

   public RecipientPK() {
      super();
   }

   @ManyToOne
   public long getTicketId() {
      return ticketId;
   }

   public void setTicketId(long ticketId) {
      this.ticketId = ticketId;
   }

   public long getUserId() {
      return userId;
   }

   public void setUserId(long userId) {
      this.userId = userId;
   }

   public boolean equals(Object o) {
   ...
   }

   public int hashCode() {
   ...
   }
}


Recipient.java
Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@org.hibernate.annotations.AccessType("property")
public class Recipient extends IdentityData {
   
   public enum RecipientType {TO, CC, BCC}
   
   private RecipientPK id;
   private RecipientType recipientType;

   public Recipient() {
      super();
   }

   @EmbeddedId
   public RecipientPK getId() {
      return id;
   }
   public void setId(RecipientPK id) {
      this.id = id;
   }
   
   public RecipientType getRecipientType() {
      return recipientType;
   }
   public void setRecipientType(RecipientType recipientType) {
      this.recipientType = recipientType;
   }
   
}


Ticket.java

Code:
   public enum TicketType { USER_TO_USER, USER_TO_SYSTEM }
   
   public enum TicketStatus { OPEN, CLOSED }
   
   public enum Urgency { NORMAL, URGENT }
   
   
   // unique ticket id
   private long id;
   // the parent ticket. 0=no parent
   private long parentTicketId;
   // userId who opened the ticket
   private long userId;
   private TicketType ticketType;
   private TicketStatus ticketStatus;
   private Urgency urgency;
   private String subject;
   private String body;
   // the labels this ticket belongs to
//   private Set<Label> labels;
   private Set<Recipient> recipients;
      
   public Ticket() {
      super();
   }
   
   public Ticket(long id, long parentTicketId, long userId, TicketType ticketType,
         TicketStatus ticketStatus, Urgency urgency, String subject, String body) {
      super();
      this.id = id;
      this.parentTicketId = parentTicketId;
      this.userId = userId;
      this.ticketType = ticketType;
      this.ticketStatus = ticketStatus;
      this.urgency = urgency;
      this.subject = subject;
      this.body = body;
   }
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }

   ... getters & setters here ....

   @OneToMany(mappedBy="ticketId")
   @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   public Set<Recipient> getRecipients() {
      return recipients;
   }

   public void setRecipients(Set<Recipient> recipients) {
      this.recipients = recipients;
   }


also code for IdentityData.java which all Entities inherit from
Code:
@Embeddable
public abstract class IdentityData {
   // date row created
   private Date createDate;
   // timestamp: row changed
   private Date updateDate;
   // ID of user last modified the row
   private long operatorId;
   // IP of user last modified the row
   private String operatorIp;

   public IdentityData() {
      super();
   }

   ... getters & setters here ....



The error I'm getting is:
java.lang.NoSuchMethodError: javax.persistence.ManyToOne.targetEntity()Ljava/lang/Class;
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1361)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at hibernate.util.HibernateUtil.createSessionFactory(HibernateUtil.java:24)
at hibernate.util.Struts2Dispatcher.init(Struts2Dispatcher.java:19)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3696)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)


anyone has a clue what am I doing wrong?


Top
 Profile  
 
 Post subject: Re: ManyToOne from inside EmbeddedId - code attached
PostPosted: Tue Sep 15, 2009 3:47 am 
Newbie

Joined: Sun Aug 30, 2009 11:04 am
Posts: 4
SOLVED

I used the
Code:
@CollectionOfElements
property instead of the
Code:
@ManyToOne
, like this:

Ticket.java
Code:
...
@CollectionOfElements(fetch = FetchType.EAGER)
   public Set<RecipientElement> getRecipients() {
      return this.recipients;
   }
...


RecipientElement.java
Code:
@Embeddable
public class RecipientElement implements java.io.Serializable {
...



and I autogenerated the SQL DB code using hibernate tools, which actually created a very efficient and effective code.


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