Hello! I'm starting with Object Relational Mapping and I'm having trouble trying to create a relationship.
I have two tables in the database called "base_dados":
+----------------------+
| Tables_in_base_dados |
+----------------------+
| grouptable |
| usertable |
+----------------------+
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| userid | varchar(10) | NO | PRI | | |
| groupid | varchar(20) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| userid | varchar(10) | NO | PRI | NULL | |
| password | varchar(64) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
The only foreign key I have is in grouptable, and it is linking the column "userid" in grouptable to "userid" in usertable.
The code that I'm using is:
@Entity
@Table(name="usertable")
public class Usertable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private String userid;
private String password;
//bi-directional many-to-one association to Grouptable
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="userid", referencedColumnName="userid")
private Grouptable grouptable;
public Usertable() {
}
public String getUserid() {
return this.userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Grouptable getGrouptable() {
return this.grouptable;
}
public void setGrouptable(Grouptable grouptable) {
this.grouptable = grouptable;
}
}
@Embeddable
public class GrouptablePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private String userid;
private String groupid;
public GrouptablePK() {
}
public String getUserid() {
return this.userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getGroupid() {
return this.groupid;
}
public void setGroupid(String groupid) {
this.groupid = groupid;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof GrouptablePK)) {
return false;
}
GrouptablePK castOther = (GrouptablePK)other;
return
this.userid.equals(castOther.userid)
&& this.groupid.equals(castOther.groupid);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.userid.hashCode();
hash = hash * prime + this.groupid.hashCode();
return hash;
}
}
@Entity
@Table(name="grouptable")
public class Grouptable implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private GrouptablePK id;
//bi-directional many-to-one association to Usertable
@OneToMany(mappedBy="grouptable", cascade={CascadeType.ALL})
private List<Usertable> usertables;
public Grouptable() {
}
public GrouptablePK getId() {
return this.id;
}
public void setId(GrouptablePK id) {
this.id = id;
}
public List<Usertable> getUsertables() {
return this.usertables;
}
public void setUsertables(List<Usertable> usertables) {
this.usertables = usertables;
}
}
And I'm getting the error
cannot Deploy login_new_teste
Deployment Error for module: login_new_teste: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [login_new_teste] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field grouptable] from the entity class [class entity.Usertable] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.. Please see server.log for more details.
To me there is nothing wrong because I linked the only foreign key in the class Usertable.java , but obviously I'm worng. Anyone can help me?