在Java EE 6应用程序中使用entityManager.merge进行更新

时间:2014-08-04 08:18:56

标签: merge java-ee-6 entitymanager insert-update

我使用entityManager.merge(obj)在我的应用程序中更新。但它不是更新对象,而是插入新记录。请帮我解决这个问题。

这是我的servlet:

@WebServlet(name = "PutAuthor", urlPatterns = {"/PutAuthor"})
public class PutAuthor extends HttpServlet {
    @EJB
    private AuthorEntityFacade authorEntityFacade;
    @Resource(mappedName="jms/UpdateAuthorFactory")
    private ConnectionFactory connectionFactory;
    @Resource(mappedName="jms/UpdateAuthor")
    private Queue queue;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String description=request.getParameter("description");
        if ((name!=null) && (description!=null)) {
            try {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
                MessageProducer messageProducer = session.createProducer(queue);
                ObjectMessage message = session.createObjectMessage();
                // here we create NewsEntity, that will be sent in JMS message
                AuthorEntity e = new AuthorEntity();
                e.setAuthorName(name);
                e.setDescription(description);
                message.setObject(e);
                messageProducer.send(message);
                messageProducer.close();
                connection.close();
                response.sendRedirect("ListAuthors");
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }

        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Update Author</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Update Author</h1>");
            AuthorEntity author=(AuthorEntity)authorEntityFacade.find(Long.parseLong(id));
            out.println("<form>");
            out.println("Author ID      : <input type='number' name='id' value='"+id+"' readonly><br/>");
            out.println("Author Name    : <input type='text' name='name' value='"+author.getAuthorName()+"'><br/>");
            out.println("Description    : <textarea name='description'>"+author.getDescription()+"</textarea><br/>");
            out.println("<input type='submit'><br/>");
            out.println("</form>"); 
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

这是我的消息驱动bean:

@MessageDriven(mappedName = "jms/UpdateAuthor", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class UpdateAuthor implements MessageListener {
    @Resource
    private MessageDrivenContext mdc;
    @PersistenceContext(unitName = "IT11032748_Book_Catalogue-ejbPU")
    private EntityManager em;

    public UpdateAuthor() {
    }

    @Override
    public void onMessage(Message message) {
        ObjectMessage objectMessage=null;
        try{
            if(message instanceof ObjectMessage){
                objectMessage=(ObjectMessage)message;
                AuthorEntity author=(AuthorEntity)objectMessage.getObject();
                update(author);
            }
        }catch(JMSException e){
            e.printStackTrace();
            mdc.setRollbackOnly();
        }catch(Throwable te){
            te.printStackTrace();
        }
    }

    public void update(Object object) {
        em.merge(object);
    }
}

请检查出错的地方。

1 个答案:

答案 0 :(得分:0)

没有问题。合并将创建对象(如果它是新的)或更新对象(如果不是)。如果您需要更多帮助,则需要在问题中添加更多详细信息。