实体类 [...] 中带注释的元素 [...] 上的 @JoinColumns 不完整

发布于 2024-09-13 22:06:02 字数 2793 浏览 2 评论 0原文

我使用:NetBeans IDE 6.7.1、GlassFish v2.1、Oracle 10g XE、JAVA 6 SE、JAVA 5 EE。

我对 @ManyToMany 注释有问题:

@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>();

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.

创建数据库的脚本部分:

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
)

Customer 类的代码

@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;
    }
...
}

错误地我将 CustRentMovie 类而不是 Movie So 作为类型放入集合中我将其更改

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

private Collection<Movie> movies = new ArrayList<Movie>();

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>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-09-20 22:06:02

CustomerRentMovie 的 PK 由三列组成(title、cust_id、rent_date)。 Toplink 表明您需要在 @JoinColumn 注释中将所有三个指定为连接列(当前您仅指定了 cust_id)。

将注释更改为类似的内容应该可以帮助您克服此错误。

@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>();

也就是说,帕斯卡的问题是有效的 - 看起来您打算建立从客户到电影的关系,而不是客户到 CUST_RENT_MOVIE 的关系。

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.

つ低調成傷 2024-09-20 22:06:02

我错误地将 CustRentMovie 类而不是 Movie 作为类型放入集合中,所以我将其更改

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

private Collection<Movie> movies = new ArrayList<Movie>();

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文