-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate and the Java collections interfaces
PostPosted: Mon Dec 04, 2006 1:03 am 
Newbie

Joined: Sat Dec 02, 2006 10:55 pm
Posts: 10
Hey guys, im pretty new at using hibernate and im wondering how can a List or any java collections object be mapped, using Annotations in particular, say ive got the following two classes at the moment, can anyone PLEASE!! tell whats wrong with this:

Code:
package piransPackage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity

public class Worker implements Serializable{
   
   int id;
   String name;
   List<Tool> tools;
   
   
   public Worker(){
      tools = new ArrayList<Tool>();
   }
   
   public Worker(int id, String name){
      this.setId(id);
      this.setName(name);
      tools = new ArrayList<Tool>();      
   }

   
   @Id   
   public int getId() {
      return id;
   }

   
   public void setId(int id) {
      this.id = id;
   }

   @Column
   public String getName() {
      return name;
   }

   
   public void setName(String name) {
      this.name = name;
   }

   /**
    * @return the tools
    */
   @OneToMany (mappedBy="id", cascade = CascadeType.ALL)
   @OrderBy("toolName")
   public List<Tool> getTools() {
      return tools;
   }

   /**
    * @param tools the tools to set
    */
   public void setTools(List<Tool> tools) {
      this.tools = tools;
   }

}



Code:
package piransPackage;

import java.io.*;
import javax.persistence.*;


@Entity
public class Tool implements Serializable{
   int id;
   String toolName;
   
   public Tool(int id, String toolName) {
      this.id = id;
      this.toolName = toolName;
   }      
   
   public Tool() {
   }


   /**
    * @return the id
    */
   @Id
   public int getId() {
      return id;
   }
   /**
    * @param id the id to set
    */
   public void setId(int id) {
      this.id = id;
   }
   /**
    * @return the toolName
    */
   @Column
   public String getToolName() {
      return toolName;
   }
   /**
    * @param toolName the toolName to set
    */
   public void setToolName(String toolName) {
      this.toolName = toolName;
   }

   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   @Override
   public int hashCode() {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + id;
      result = PRIME * result + ((toolName == null) ? 0 : toolName.hashCode());
      return result;
   }

   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final Tool other = (Tool) obj;
      if (id != other.id)
         return false;
      if (toolName == null) {
         if (other.toolName != null)
            return false;
      } else if (!toolName.equals(other.toolName))
         return false;
      return true;
   }
   
   public String toString(){
      return "Tool: id - " + this.getId() + " name - " + this.getToolName();
   }

   

}


YEAH ive been breakign my head with this for maybe a day!! X_X


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.