0

I make use of: NetBeans IDE 6.7.1, GlassFish v2.1, Oracle 10g XE, JAVA 6 SE, JAVA 5 EE.

I have problem with an @ManyToMany annotation:

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="CUST_RENT_MOVIE", joinColumns={@JoinColumn(name="CUST_ID")}, inverseJoinColumns={@JoinColumn(name="TITLE")})
private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>();

part of the output of the GlassFish v2.1.1

Exception Description: The @JoinColumns on the annotated element [private java.util.Collection vc.domain.Customer.rents] from the entity class [class vc.domain.Customer] 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 referenceColumnName elements must be specified in each such @JoinColumn.
javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.1 (Build b31g-fcs (10/19/2009))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [vc_pu] failed.

part of the script that create the database:

CREATE table customer
(
cust_id NUMBER(5),    
CONSTRAINT cust_pk PRIMARY KEY (cust_id),
...
)

CREATE TABLE movie
(
title VARCHAR2(50) PRIMARY KEY,
...
)

CREATE TABLE cust_rent_movie
(
title VARCHAR2(50),
cust_id NUMBER(5),
rent_date DATE DEFAULT current_date NOT NULL,
return_date DATE,
CONSTRAINT cust_rent_movie_pk PRIMARY KEY (title, cust_id, rent_date),
CONSTRAINT CustRentMovie_movie_fk FOREIGN KEY (title) REFERENCES movie ON DELETE CASCADE,
CONSTRAINT CustRentMovie_cust_fk FOREIGN KEY (cust_id) REFERENCES customer ON DELETE CASCADE
)

the code of the Customer class

@Entity
@Table(name = "customer")
@SequenceGenerator(name="seq", sequenceName="cust_id_seq", allocationSize=1)
public class Customer implements Serializable
{

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
    @Column(name="CUST_ID")
    private int id;


    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="CUST_RENT_MOVIE", joinColumns={@JoinColumn(name="CUST_ID")}, inverseJoinColumns={@JoinColumn(name="TITLE")})
    private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>();


    public Collection<CustRentMovie> getRents()
    {
        return rents;
    }

    public void setRents(Collection<CustRentMovie> rents)
    {
        this.rents = rents;
    }
...
}

by mistake I put as type in the collection the class CustRentMovie instead of Movie So I changed the

private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>();

to

private Collection<Movie> movies = new ArrayList<Movie>();
IVANNHS
  • 13
  • 1
  • 4
  • Just to be sure, do you really want to map a many-to-many association between `Customer` and `CustRentMovie` (or a many-to-many between `Customer` and `Movie`)? – Pascal Thivent Aug 11 '10 at 21:35
  • I want to map a many-to-many association between Customer and Movie. CustRentMovie is the join table. The association has as properties the day of red and the day of the return. Because a customer may rent the same movie more than one i added the red_date to the primary key. – IVANNHS Aug 12 '10 at 04:54
  • Finally I finished my first JEE project, it was a nightmare Every now and then countless sheets of exception. Thanks for the help. – IVANNHS Aug 13 '10 at 19:04

2 Answers2

1

CustomerRentMovie's PK is made up of three columns (title, cust_id, rent_date). Toplink is indicating that you need to specify all three as join columns in your @JoinColumn annotation (currently you have specified only cust_id).

Changing the annotation to something like this should get you past this error.

@JoinTable(name = "CUST_RENT_MOVIE", joinColumns = {
    @JoinColumn(name = "CUST_ID"),
    @JoinColumn(name = "TITLE"),
    @JoinColumn(name = "RENT_DATE") }, inverseJoinColumns = { @JoinColumn(name = "TITLE") })
private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>();

That said, Pascal's question is valid - it looks like you intended to have a relationship from Customer to Movie, not Customer to CUST_RENT_MOVIE.

Mike
  • 18,694
  • 1
  • 20
  • 10
0

by mistake I put as type in the collection the class CustRentMovie instead of Movie So I changed the

private Collection<CustRentMovie> rents = new ArrayList<CustRentMovie>();

to

private Collection<Movie> movies = new ArrayList<Movie>();
IVANNHS
  • 13
  • 1
  • 4
  • Do you mean to set this answer as my accepted answer? or I have to do something else. – IVANNHS Aug 13 '10 at 05:28
  • No, that's not what I mean - I think Mike's answer is a better candidate :) What I mean is using the `[edit]` link at the bottom of your initial question to edit its content (don't you have that `[edit]` link?). – Pascal Thivent Aug 13 '10 at 16:59