java - 映射到实体时出现 JPA 异常

标签 java jpa

您好,我正在编写我的第一个 JPA 项目,我是这个领域的新手,所以请尽量简化内容 :) 这是实体之一

package model;

import java.io.Serializable;
import java.sql.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity

public class Speaker implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    private Date dob;
    private String skills;
    @OneToMany(mappedBy="event",cascade={CascadeType.ALL})
    private Set <Session> speakerSessions;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the dob
     */
    public Date getDob() {
        return dob;
    }
    /**
     * @param dob the dob to set
     */
    public void setDob(Date dob) {
        this.dob = dob;
    }
    /**
     * @return the skills
     */
    /**
     * @return the skills
     */
    public String getSkills() {
        return skills;
    }
    /**
     * @param skills the skills to set
     */
    public void setSkills(String skills) {
        this.skills = skills;
    }
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */

}

这是抛出的异常

   Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [test] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [EVENT] is not present in this descriptor.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Runtime Exceptions: 
---------------------------------------------------------

    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:816)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:756)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    at Test.Tester.main(Tester.java:19)
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [test] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [EVENT] is not present in this descriptor.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Runtime Exceptions: 
---------------------------------------------------------

    at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:238)
    ... 7 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [EVENT] is not present in this descriptor.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(model.Speaker --> [DatabaseTable(SPEAKER)])

Runtime Exceptions: 
---------------------------------------------------------

    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:689)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:625)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:565)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:792)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:736)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:681)
    ... 5 more

我在这方面的经验几乎为零,所以请提供尽可能多的详细信息:)

这是 session 实体伙计们

package model;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table (name="session")
@NamedQueries({
@NamedQuery (name="findAllSessionForEvent",query="select e from Session e where e.event.id= :id"),
@NamedQuery (name="findSessionsBySpeakerName",query="select e from Session e where e.speaker.name=':name'"),
@NamedQuery (name="findSpeakerOfSessionBySessionId",query="select e.speaker.name from Session e where e.id=:id")})
@XmlRootElement

public class Session implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private int id;
    private Date date;
    @ManyToOne
    @JoinColumn (name="eventID")
    //@Column(name="eventID")
    private Event event;
    @ManyToOne
    @JoinColumn (name="speakerID")
    //@Column(name="speakerID")
    private Speaker speaker;
    /**
     * @return the date
     */
    public Date getDate() {
        return date;
    }
    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Speaker getSpeaker() {
        return speaker;
    }
    public void setSpeaker(Speaker speaker) {
        this.speaker = speaker;
    }
    public Event getEvent() {
        return event;
    }
    public void setEvent(Event event) {
        this.event = event;
    }


}

最佳答案

如果你有 Session.speaker 那么为什么 Speaker.speakerSessionsma​​ppedBy=event ???它应该是“扬声器”。 MappedBy 基本上说明了关系另一端的字段是什么

关于java - 映射到实体时出现 JPA 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727724/

相关文章:

java - 在bean中注入(inject)多个远程EJB

java - Mysql 复杂对象 Hibernate 批量插入

java - 我怎样才能让这个程序将用户输入添加到我的链接列表中?

java - map 的 BatchedTooManyRowsAffectedException

java - JPA:如何使用 OneToMany 注释将新项目添加到列表中

java - 从 reducer 访问映射器的计数器

java - 克隆对象也会克隆新数据,如何防止这种情况发生?

java - springMvc 和 hibernate 持久化

java - 绑定(bind) native 查询错误。 EclipseLink(JPA 2.1)

java - 使用@OneToOne属性设置对象可以,但不适用于@ManyToMany对象