我按照此网址关注教程; http://wiki.netbeans.org/DevelopJavaEE6App。
当我在以下几行收到错误时,我会陷入困境;
Query query = em.createNamedQuery("Customer.findAll");
return query.getResultList();
错误如下:
" incompatible types
required: javax.management.Query
found: javax. persistence.Query"
AND SECONDLY
" cannot find symbol
symbol: method getResultList()
location: class javax.management.Query"
我不明白这些错误,因为我有以下导入。
javax.management.Query中;
这是我的文件的完整代码 - CustomerSession.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.customerapp.ejb;
import com.customerapp.entity.Customer;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
/**
*
* @author Padjester
*/
@Stateless
@LocalBean
public class CustomerSession {
@PersistenceContext
private EntityManager em;
/**
* Returns a list of customer objects in the database *
* @return List<Customer>
*/
public List<Customer> retrieve() {
Query query = em.createNamedQuery("Customer.findAll");
return query.getResultList();
}
/**
*Update the customer record
* @param customer object to be updated
* @return Customer
*/
@Resource(name = "jms/NotificationQueue")
private Queue notificationQueue;
@Resource(name = "jms/NotificationQueueFactory")
private ConnectionFactory notificationQueueFactory;
public Customer update(Customer customer) {
Customer updated = em.merge(customer);
try {
sendJMSMessageToNotificationQueue(updated);
} catch (JMSException ex) {
Logger.getLogger(CustomerSession.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Customer updated in CustomerSession!");
return updated;
}
private Message createJMSMessageForjmsNotificationQueue(Session session, Object messageData) throws JMSException {
//Modified to use ObjectMessage instead
ObjectMessage tm = session.createObjectMessage();
tm.setObject((Serializable) messageData);
return tm;
}
private void sendJMSMessageToNotificationQueue(Object messageData) throws JMSException {
Connection connection = null;
Session session = null;
try {
connection = notificationQueueFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(notificationQueue);
messageProducer.send(createJMSMessageForjmsNotificationQueue(session, messageData));
} finally {
if (session != null) {
try {
session.close();
} catch (JMSException e) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
}
}
if (connection != null) {
connection.close();
}
}
}
}
如果您不介意,请查看我的代码并相应地给我建议。
谢谢 亲切的问候
答案 0 :(得分:2)
错误消息说明您导入错误。
替换
行import javax.management.Query;
与
import javax.persistence.Query;