Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Junction table with additional attribute; incompattible mapping

Hi B.J.!

I think you can not use @Id and @ManyToOne together to the same property.

Read this reference: 
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Example_ManyToOne_id_annotation
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Example_ManyToOne_id_annotation 

I have a similar problem and I have not solved yet (
http://www.nabble.com/Error-Mapping-a-Join-Table-with-Additional-Columns-td23556016.html
http://www.nabble.com/Error-Mapping-a-Join-Table-with-Additional-Columns-td23556016.html
)

However, the recommendation is to use a separate identifier for the join
table

Regards

Daniel R. Criado




bhardage wrote:
> 
> I have 2 tables:
> 
> Movies
> movieID
> 
> Users
> userID
> 
> These tables have a many to many relationship through the Queue table,
> with an additional attribute, listOrder:
> 
> Queue
> movieID
> userID
> listOrder
> 
> I'm attempting to model this using EclipseLink, but am getting an
> "incompattible mapping" error. Here is a sampling of my code:
> 
> @Entity
> @Table(name="movieinventory")
> public class Movie implements Serializable
> {
> 	private static final long serialVersionUID = 1L;
> 
> 	@Id
> 	@GeneratedValue
> 	private Integer movieID;
> 	
> 	@OneToMany(mappedBy="movie")
> 	private Set<QueueItem> moviesInQueue;
> 
>         ...Getters/Setters...
> }
> 
> @Entity
> @Table(name="Users")
> public class User implements Serializable
> {
> 	private static final long serialVersionUID = 1L;
> 
> 	@Id
> 	@GeneratedValue
> 	private Integer userID;
> 	
> 	@OneToMany(mappedBy="user")
> 	private Set<QueueItem> moviesInQueue;
> 
>        ...Getters/Setters...
> }
> 
> @IdClass(QueueItemPK.class)
> @Entity
> @Table(name="queue")
> public class QueueItem
> {	
> 	@Id
> 	@ManyToOne
> 	@JoinColumn(name="movieID")
> 	private Movie movie;
> 	
> 	@Id
> 	@ManyToOne
> 	@JoinColumn(name="userID")
> 	private User user;
> 	
> 	@Basic
> 	private String listOrder;
> 
>         ...Getters/Setters...
> }
> 
> public class QueueItemPK implements Serializable
> {
> 	private static final long serialVersionUID = 1L;
> 
> 	private Movie movie;
>         private User user;
> 
>         ...Getters/Setter...
> 
> 	public int hashCode()
> 	{
>             return (String.valueOf(movie) +
> String.valueOf(user)).hashCode();
>         }
> 
>         public boolean equals(Object obj)
>         {
>             if (obj == this) return true;
>             if (obj == null) return false;
>             if (!(obj instanceof QueueItemPK)) return false;
>             QueueItemPK pk = (QueueItemPK) obj;
>             return pk.movie == movie && pk.user == user;
>         }
> }
> 
> The purpose of the QueueItemPK is so that I can have the composite primary
> key of movieID and userID. I'm not absolutely sure this is the correct way
> to do it.
> 
> This is the error:
> Exception Description: An incompatible mapping has been encountered
> between [class Movie] and [class QueueItem]. This usually occurs when the
> cardinality of a mapping does not correspond with the cardinality of its
> backpointer.
> 
> When I take the @Id annotations off of the movie and user variables in
> QueueItem and make some other key the primary key, however, it compiles
> with no errors.
> 
> Any suggestions would be appreciated.
> 
> Thanks,
> B.J.
> 

-- 
View this message in context: http://www.nabble.com/Junction-table-with-additional-attribute--incompattible-mapping-tp23598111p23611764.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top