Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem in Object Relational Mapping with @JoinColumn
icon4.gif  Problem in Object Relational Mapping with @JoinColumn [message #898825] Fri, 27 July 2012 15:13 Go to next message
Clarisse Missing name is currently offline Clarisse Missing name
Messages: 1
Registered: July 2012
Junior Member
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?
Re: Problem in Object Relational Mapping with @JoinColumn [message #899097 is a reply to message #898825] Mon, 30 July 2012 10:32 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
JPA requires using the full primary key when linking entities. That means JPA requires the Usertable to have 2 foriegn keys to Grouptable - a foreign key to UserId and groupid so that it can uniquely identify the Grouptable. You would also get an exception because you have the userId in Usertable mapped in two separate but writable mappings.

I am not sure your entity model matches what you have set up in the database. Since GroupTable has the UserId, it really can only reference a single Usertable entity at a time - a 1:1 or M:1 relationship would make more sense to me. As many Grouptable instances can reference the same Usertable, Usertable could have a 1:m relationship back to GroupTable.

Best Regards,
Chris
Previous Topic:[SOLVED] Entity Class identity problem in OSGi after uninstalling/reinstalling persistence bundle
Next Topic:Creating XML where the namespaces differ in different packages
Goto Forum:
  


Current Time: Thu Jul 31 09:44:19 EDT 2014

Powered by FUDForum. Page generated in 0.01634 seconds