根据axtavt的回答,这几乎肯定是Notebean和NoteBean之间的命名问题。这里有一个特定的惯例,只是普通的CamelCase吗?
我相信我已经更新了实体中的@NamedQuery注释并使用了控制器中的注释,并且名称看起来匹配,但我仍然收到关于模式的相同错误,我不能获得更多信息。
JPA控制器:
package net.bounceme.dur.nntp.controller;
import java.util.*;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.persistence.*;
import javax.swing.DefaultListModel;
import net.bounceme.dur.nntp.model.NoteBean;
public class NotesController {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(NotesController.class.getName());
private Message message;
private List<NoteBean> notes = new ArrayList<NoteBean>();
private DefaultListModel defaultListModel = new DefaultListModel();
private EntityManagerFactory emf;
private EntityManager em;
private String PERSISTENCE_UNIT_NAME = "nntpPU";
public NotesController() {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = emf.createEntityManager();
LOG.info("entity manager made???" + em.isOpen());
populateList();
}
private void populateList() {
LOG.info("open?" + em.isOpen());
em.getTransaction().begin();
LOG.info("trying to populate.....");
//Query q = em.createQuery("SELECT n FROM NoteBean n WHERE n.id = :id");
Query q = em.createNamedQuery("NoteBean.findAll");
LOG.info(q.toString());
List results = q.getResultList();
em.getTransaction().commit();
setNotes(results);
DefaultListModel dlm = new DefaultListModel();
for (NoteBean n : getNotes()) {
dlm.addElement(n);
}
setDefaultListModel(dlm);
}
public Message getMessage() {
return message;
}
public void addNote(NoteBean noteBean) {
LOG.info(noteBean.toString());
em.getTransaction().begin();
em.persist(noteBean);
em.getTransaction().commit();
populateList();
}
public void setMessage(Message message) {
this.message = message;
}
private List<NoteBean> getNotes() {
return notes;
}
private void setNotes(List<NoteBean> notes) {
this.notes = notes;
}
public DefaultListModel getDefaultListModel() {
return defaultListModel;
}
public void setDefaultListModel(DefaultListModel defaultListModel) {
this.defaultListModel = defaultListModel;
}
}
实体:
package net.bounceme.dur.nntp.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "NOTEBEAN", catalog = "nntp", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "NoteBean.findAll", query = "SELECT n FROM NoteBean n"),
@NamedQuery(name = "NoteBean.findById", query = "SELECT n FROM Notebean n WHERE n.id = :id"),
@NamedQuery(name = "NoteBean.findByStamp", query = "SELECT n FROM Notebean n WHERE n.stamp = :stamp"),
@NamedQuery(name = "NoteBean.findByNote", query = "SELECT n FROM Notebean n WHERE n.note = :note")
})
public class NoteBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "STAMP")
@Temporal(TemporalType.DATE)
private Date stamp;
@Column(name = "NOTE", length = 255)
private String note;
public NoteBean() {
}
public NoteBean(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getStamp() {
return stamp;
}
public void setStamp(Date stamp) {
this.stamp = stamp;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof NoteBean)) {
return false;
}
NoteBean other = (NoteBean) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "net.bounceme.dur.nntp.model.Notebean[ id=" + id + " ]";
}
}
运行时错误:
init:
Deleting: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/SwingNNTP/build/built-jar.properties
Compiling 2 source files to /home/thufir/NetBeansProjects/SwingNNTP/build/classes
warning: [options] bootstrap class path not set in conjunction with -source 1.5
Note: /home/thufir/NetBeansProjects/SwingNNTP/src/net/bounceme/dur/nntp/controller/NotesController.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
compile:
run:
May 03, 2012 7:23:05 AM net.bounceme.dur.nntp.PropertiesReader getProps
INFO: NNTP.loadMessages...
May 03, 2012 7:23:06 AM net.bounceme.dur.nntp.controller.MessagesEnum <init>
INFO: starting controller..
[TopLink Info]: 2012.05.03 07:23:10.319--ServerSession(10922033)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))
Exception in thread "AWT-EventQueue-0" Local Exception Stack:
Exception [TOPLINK-8034] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: Error compiling the query [NoteBean.findByNote: SELECT n FROM Notebean n WHERE n.note = :note]. Unknown abstract schema type [Notebean].
at oracle.toplink.essentials.exceptions.EJBQLException.unknownAbstractSchemaType(EJBQLException.java:494)
at oracle.toplink.essentials.internal.parsing.ParseTreeContext.classForSchemaName(ParseTreeContext.java:163)
at oracle.toplink.essentials.internal.parsing.SelectNode.getClassOfFirstVariable(SelectNode.java:366)
at oracle.toplink.essentials.internal.parsing.SelectNode.getReferenceClass(SelectNode.java:354)
at oracle.toplink.essentials.internal.parsing.ParseTree.getReferenceClass(ParseTree.java:463)
at oracle.toplink.essentials.internal.parsing.ParseTree.adjustReferenceClassForQuery(ParseTree.java:103)
at oracle.toplink.essentials.internal.parsing.EJBQLParseTree.populateReadQueryInternal(EJBQLParseTree.java:127)
at oracle.toplink.essentials.internal.parsing.EJBQLParseTree.populateQuery(EJBQLParseTree.java:108)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:219)
at oracle.toplink.essentials.queryframework.EJBQLPlaceHolderQuery.processEjbQLQuery(EJBQLPlaceHolderQuery.java:111)
at oracle.toplink.essentials.internal.sessions.AbstractSession.processEJBQLQueries(AbstractSession.java:2059)
at oracle.toplink.essentials.internal.sessions.AbstractSession.processEJBQLQueries(AbstractSession.java:2046)
at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:724)
at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:604)
at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:280)
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:229)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:93)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:126)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:120)
at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:91)
at net.bounceme.dur.nntp.controller.NotesController.<init>(NotesController.java:23)
at net.bounceme.dur.nntp.gui.Detail.<init>(Detail.java:20)
at net.bounceme.dur.nntp.gui.NewFrame.initComponents(NewFrame.java:30)
at net.bounceme.dur.nntp.gui.NewFrame.<init>(NewFrame.java:17)
at net.bounceme.dur.nntp.gui.NewFrame$2.run(NewFrame.java:62)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 10 seconds)
仅供参考,mysql控制台输出:
mysql> describe nntp.NOTEBEAN;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| ID | bigint(20) | NO | PRI | NULL | |
| STAMP | date | YES | | NULL | |
| NOTE | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
mysql> select * from nntp.NOTEBEAN;
Empty set (0.04 sec)
mysql>
答案 0 :(得分:8)
您的实体类的名称(因此,在查询中使用的实体的默认逻辑名称)是NoteBean
,而stacktrace则抱怨Notebean
。
因此,您应该在查询中使用NoteBean
而不是Notebean
:
@NamedQuery(name = "Notebean.findById",
query = "SELECT n FROM NoteBean n WHERE n.id = :id")