[英]JPA exception while mapping to entities
嗨,我正在編寫我的第一個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
我幾乎沒有這方面的經驗,所以請提供盡可能詳細的信息:)
這是會話實體的人
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.speakerSessions
有mappedBy = event ??? 它應該是“ 發言者 ”。 MappedBy基本上說明了關系另一邊的字段是什么
您必須在底層數據庫中具有正確的架構結構,並且必須在JPA實體中具有正確的映射。
對於正確的模式,您可以從JPA供應商生成模式開始。 您可以通過向persistence.xml文件添加一個屬性來實現此目的:
如果您使用的是EclipseLink:
<property name="eclipselink.ddl-generation" value="create-tables" />
如果你正在使用Hibernate:
<property name="hibernate.hbm2ddl.auto" value="create" />
使用它們你應該擺脫A non-read-only mapping must be defined for the sequence number field.
錯誤。 此錯誤告訴您JPA提供程序沒有從中生成ID的表。
您的第二個錯誤The table [EVENT] is not present in this descriptor.
可能意味着@OneToMany
映射到Session
的問題。 Session是一個實體,是否有正確的@ManyToOne
映射回Speaker
?
錯誤說 - “必須為序列號字段定義非只讀映射。”
嘗試在Speaker.java中為“Id”字段添加setter
IE瀏覽器。 將此代碼添加到Speaker.java -
public void setId(int id) {
this.id = id;
}
異常描述:此描述符中不存在表[EVENT]。
加
@Table(name="Schamaname.Tablename")
要么
@Table(name = "Tablename", schema = "Schamaname")
為id添加getter和setter。 如果你得到NullPointerException初始化id與構造函數
public Speaker (int id) {
this.id = id
}
或者將int更改為Integer,因為int在java中不能為“null”,但是至於它是主鍵,它在數據庫中永遠不應該為null。
您可能還需要@NamedQueries。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.