廣告

2016年5月19日 星期四

Hibernate  one-to-one mapping cautions


正確用法
@OneToOne(mappedBy = "post")
@JsonView(View.Detail.class)
private HotPost hotPost;

一開始犯傻
@OneToOne(mappedBy = "post")
@JsonView(View.Detail.class)
Set hotpost = new HashSet<>(0);
CautionsCautionsCautions
log只會說:Unknown mappedBy in: com.ws.pojo.Post.hotPost, referenced property unknown: java.util.Set.post


but @OneToMany
remember to use Set


ref:




Hibernate batch process


 //in pojo (java bean)
      @BatchSize(size=5)
------------------------------------------------
      public void insertOldPost2HotPost() throws Exception{
       
       Session session = sessionFactory.getCurrentSession();

        @SuppressWarnings("unchecked")
        List posts = session.createQuery("from Post ").list();
        int itCount=0;
        for(Iterator it = posts.iterator(); it.hasNext(); ) {

            itCount++;

            Post post = (Post) it.next();

            HotPost hotPost = new HotPost(post);
            session.saveOrUpdate(hotPost);

            if (itCount % 5 == 0) {
            
                session.flush();
                session.clear();
            }
        }
    }


ref:


2016年5月10日 星期二

Spring MVC @JsonView使用详解


     public class View {
      interface Summary {}
      interface SummaryWithDetail extends Summary{}
    }
    -----------------------------------
    public class User {
      @JsonView(View.Summary.class)
      private Long id;
      @JsonView(View.SummaryWithDetail.class)
      private String firstname;
    }
    -----------------------------------
    @RequestMapping("/user")[
    @JsonView(View.Summary.class)
    //or @JsonView(View.SummaryWithDetail.class)
    public List getUsers(){
     return userService.listUsers();
    }
    -----------------------------------
    result of @JsonView(View.Summary.class)
    [
     {
        "id": 70,
     }
    ]

    result of @JsonView(View.SummaryWithDetail.class)
    [
     {
        "id": 70,
        "firstname": 222
     }
    ]


2016年3月28日 星期一

Hibernate openSession() vs getCurrentSession()


      SessionFactory.openSession():
      always opens a new session that you have to
      close once you are done with the operations.
     
      SessionFactory.getCurrentSession():
      returns a session bound to a context - you don't need to close this. 
     
      should always use "one session per request" or "one session per transaction"
      In one application, if the DAO layer, using Spring hibernate,
      control the life cycle via Spring session to ,

      First choice  getCurrentSession ().

2016年3月17日 星期四

Usage of @JsonView (annotation of spring)


    public class View {
      interface Summary {}
      interface SummaryWithDetail extends Summary{}
    }
    -----------------------------------
    public class User { 
      @JsonView(View.Summary.class) 
      private Long id; 
      @JsonView(View.SummaryWithDetail.class) 
      private String firstname; 
    }
    -----------------------------------
    @RequestMapping("/user")
    @JsonView(View.Summary.class) 
    //or @JsonView(View.SummaryWithDetail.class) 
    public List getUsers(){
     return userService.listUsers();
    }
    -----------------------------------
    result of @JsonView(View.Summary.class)
    [
     {
        "id": 70,
     }
    ]
    
    result of @JsonView(View.SummaryWithDetail.class)
    [
     {
        "id": 70,
        "firstname": 222
     }
    ]

ref:
http://www.jianshu.com/p/633d83dd303b#

2016年3月10日 星期四

Apache_httpd_to_Tomcat-Mod_Proxy_Setup.txt | 轉址


sudo vi /opt/lampp/etc/httpd.conf

Make sure the following are uncommented:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Add these:
ProxyPass         /eInvoiceLocate http://localhost:8080/eInvoiceLocate
ProxyPassReverse  /eInvoiceLocate http://localhost:8080/eInvoiceLocate

Reload Apache conf
# cd /opt/lampp/bin
# sudo ./apachectl -k graceful


2016年2月25日 星期四

apache xmlhttprequest cannot load origin is not allowed by access-control-allow-origin |ajax 跨網頁存取圖片遇到cors

修改這個檔:
/opt/lampp/etc/httpd.conf

在 <Directory "/opt/lampp/htdocs"> 區段加入:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "x-requested-with"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"

重起
sudo /opt/lampp/bin/apachectl -k graceful

關於cors