我们如何增强持久性类

时间:2013-09-03 10:31:19

标签: spring google-app-engine google-cloud-datastore jdo persistent

我是JDO和数据存储区的新手

我已经建立了一个基于Spring Framework的简单Google App Engine项目来执行基本的CRUD操作。

当我运行我的应用程序显示

持久化类“类com.pandian.model.Customer似乎没有得到增强。您可能需要重新运行增强器并检查输出中的错误。”数据库中没有表,但操作需要它。请检查此类的MetaData规范。

客户

@PersistenceCapable
    public class Customer {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private String name;

        @Persistent
        private String email;

        @Persistent
        private Date date;

        public Key getKey() {
            return key;
        }

        public void setKey(Key key) {
            this.key = key;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public Customer() {
            super();
        }

控制器

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String getAddCustomerPage(ModelMap model) {

        return "add";

    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request, ModelMap model) {

        String name = request.getParameter("name");
        String email = request.getParameter("email");

        Customer c = new Customer();
        c.setName(name);
        c.setEmail(email);
        c.setDate(new Date());

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(c);
        } finally {
            pm.close();
        }

        return new ModelAndView("redirect:list");

    }

    @RequestMapping(value = "/update/{name}", method = RequestMethod.GET)
    public String getUpdateCustomerPage(@PathVariable String name,
        HttpServletRequest request, ModelMap model) {

        PersistenceManager pm = PMF.get().getPersistenceManager();

        Query q = pm.newQuery(Customer.class);
        q.setFilter("name == nameParameter");
        q.setOrdering("date desc");
        q.declareParameters("String nameParameter");

        try {
            @SuppressWarnings("unchecked")
            List<Customer> results = (List<Customer>) q.execute(name);

            if (results.isEmpty()) {
                model.addAttribute("customer", null);
            } else {
                model.addAttribute("customer", results.get(0));
            }
        } finally {
            q.closeAll();
            pm.close();
        }

        return "update";

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(HttpServletRequest request, ModelMap model) {

        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String key = request.getParameter("key");

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {

            Customer c = pm.getObjectById(Customer.class, key);

            c.setName(name);
            c.setEmail(email);
            c.setDate(new Date());

        } finally {

            pm.close();
        }

        // return to list
        return new ModelAndView("redirect:list");

    }

    @RequestMapping(value = "/delete/{key}", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable String key,
            HttpServletRequest request, ModelMap model) {

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {

            Customer c = pm.getObjectById(Customer.class, key);
            pm.deletePersistent(c);

        } finally {
            pm.close();
        }

PMF

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper
            .getPersistenceManagerFactory("transactions-optional");

    private PMF() {
    }

列表 // JSP

....
<%

        if(request.getAttribute("customerList")!=null){

            List<Customer> customers = 
                           (List<Customer>)request.getAttribute("customerList");

            if(!customers.isEmpty()){
                 for(Customer c : customers){

        %>
                <tr>
                  <td><%=c.getName() %></td>
                  <td><%=c.getEmail() %></td>

...

任何身体帮助我摆脱这个......

1 个答案:

答案 0 :(得分:0)

当您查看AppEngine文档以使用JDO时,您会遇到过 https://developers.google.com/eclipse/docs/appengine_orm

这告诉您如何增强用于JDO的类。