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