JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Web Framework » JSF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 JSF想抓2個資料表的值
cool3690





發文: 44
積分: 0
於 2017-10-23 08:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大家好:
想來請教各位一個問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public java.util.List<com.tiptop.db.entity.OEB> findOEBchart(java.util.Date beginDate, java.util.Date endDate,ArrayList<String> oea61,ArrayList<String> oea72){
  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");  
  String hql ;
     
      hql = "from OEB where oea.oea01=oeb01 and to_char(oea.oea72, 'YYYY-MM-DD') between '" + sdf.format(beginDate) + "' and '" + sdf.format(endDate) + "' order by to_char(oea.oea72, 'YYYY-MM-DD')  desc \n"; 
       Iterator it;      
       com.tiptop.db.entity.OEA oea ;
        com.tiptop.db.entity.OEB oeb ;
  java.util.List<com.tiptop.db.entity.OEB> oebList = new java.util.ArrayList<>();
   java.util.List<com.tiptop.db.entity.OEA> oeaList = new java.util.ArrayList<>();
  java.util.List list = this.hibernateTemplate.find(hql);
  //ArrayList<String> oea61= new ArrayList<String>();
  //ArrayList<String> oea72= new ArrayList<String>();
        it=list.iterator();  
  while (it.hasNext())
  { //oea = (com.tiptop.db.entity.OEA) it.next();
         oeb = (com.tiptop.db.entity.OEB) it.next();  
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oeb.getOeb04());    
  }
   
        return list;
    }

public java.util.List<com.tiptop.db.entity.OEB>
我知道現在我是抓oeb的資料表,目前抓了也沒問題
但我現在要參考oea資料表的值==> oea01=oeb01這樣
並且把oea某欄位的值抓進來
但一直失敗......
請問有沒有辦法只在public java.util.List<com.tiptop.db.entity.OEB>
函式內把oea和oeb的值,直接比較,判斷,並回傳呢?


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
LiaoLuke





發文: 106
積分: 0
於 2017-10-24 08:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
cool3690 wrote:
大家好:
想來請教各位一個問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public java.util.List<com.tiptop.db.entity.OEB> findOEBchart(java.util.Date beginDate, java.util.Date endDate,ArrayList<String> oea61,ArrayList<String> oea72){
  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");  
  String hql ;
     
      hql = "from OEB where oea.oea01=oeb01 and to_char(oea.oea72, 'YYYY-MM-DD') between '" + sdf.format(beginDate) + "' and '" + sdf.format(endDate) + "' order by to_char(oea.oea72, 'YYYY-MM-DD')  desc \n"; 
       Iterator it;      
       com.tiptop.db.entity.OEA oea ;
        com.tiptop.db.entity.OEB oeb ;
  java.util.List<com.tiptop.db.entity.OEB> oebList = new java.util.ArrayList<>();
   java.util.List<com.tiptop.db.entity.OEA> oeaList = new java.util.ArrayList<>();
  java.util.List list = this.hibernateTemplate.find(hql);
  //ArrayList<String> oea61= new ArrayList<String>();
  //ArrayList<String> oea72= new ArrayList<String>();
        it=list.iterator();  
  while (it.hasNext())
  { //oea = (com.tiptop.db.entity.OEA) it.next();
         oeb = (com.tiptop.db.entity.OEB) it.next();  
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oeb.getOeb04());    
  }
   
        return list;
    }

public java.util.List<com.tiptop.db.entity.OEB>
我知道現在我是抓oeb的資料表,目前抓了也沒問題
但我現在要參考oea資料表的值==> oea01=oeb01這樣
並且把oea某欄位的值抓進來
但一直失敗......
請問有沒有辦法只在public java.util.List<com.tiptop.db.entity.OEB>
函式內把oea和oeb的值,直接比較,判斷,並回傳呢?


Smile
並且把oea某欄位的值抓進來==>???您在哪段設定有取值?
您沒有取值的動作?您只有取oeb 值出來,並沒有取oea


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:LiaoLuke]
cool3690





發文: 44
積分: 0
於 2017-10-25 05:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
  
it=list.iterator();  
  while (it.hasNext())
  { //oea = (com.tiptop.db.entity.OEA) it.next();==>這行,我想把OEA的值抓出來但失敗所以碼掉
         oeb = (com.tiptop.db.entity.OEB) it.next();  
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oeb.getOeb04());    //這行我原本是寫 oea72.add(oea.getOea04());    但發現會一直錯誤........
  }
   
        return list;
    }

請問該怎麼改呢??
謝謝


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
LiaoLuke





發文: 106
積分: 0
於 2017-10-26 06:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
cool3690 wrote:
1
2
3
4
5
6
7
8
9
10
11
  
it=list.iterator();  
  while (it.hasNext())
  { //oea = (com.tiptop.db.entity.OEA) it.next();==>這行,我想把OEA的值抓出來但失敗所以碼掉
         oeb = (com.tiptop.db.entity.OEB) it.next();  
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oeb.getOeb04());    //這行我原本是寫 oea72.add(oea.getOea04());    但發現會一直錯誤........
  }
   
        return list;
    }

請問該怎麼改呢??
謝謝


錯誤>>都會有錯誤訊息
看到錯誤訊息,您還不會處理嗎?

Sleepy


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:LiaoLuke]
cool3690





發文: 44
積分: 0
於 2017-10-26 09:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
LiaoLuke大大好
他的錯誤訊息就是抓不到值
他無法在oeb裡面抓到oea的值
所以才想來請教大大能不能在
public java.util.List<com.tiptop.db.entity.OEB> findOEBchart(......){}
的函式中引進oea的值並印出來
真的想不出來,求指教,謝謝


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
snowbean71





發文: 92
積分: 0
於 2017-10-29 22:59 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的問題,應該是hql 寫錯了,檢查一下吧

reply to postreply to post
javabean71.....snowbean71......JSF+JPA+Primefaces
作者 Re:JSF想抓2個資料表的值 [Re:snowbean71]
cool3690





發文: 44
積分: 0
於 2017-10-31 03:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.....
  com.tiptop.db.entity.OEA oea =null;
      hql = "from OEB where oea.oea01=oeb01 and to_char(oea.oea72, 'YYYY-MM-DD') between '" + sdf.format(beginDate) + "' and '" + sdf.format(endDate) + "' order by to_char(oea.oea72, 'YYYY-MM-DD')  desc \n"; 
       Iterator it;      
       
        com.tiptop.db.entity.OEB oeb ;
  java.util.List<com.tiptop.db.entity.OEB> oebList = new java.util.ArrayList<>();
   java.util.List<com.tiptop.db.entity.OEA> oeaList = new java.util.ArrayList<>();
  java.util.List list = this.hibernateTemplate.find(hql);
  
        it=list.iterator();  
  while (it.hasNext())
  {
         oeb = (com.tiptop.db.entity.OEB) it.next();  
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oea.getOea61());  //改成oea就會抓不到值了  
  }

大大好,我hql沒問題耶?
只要都抓oeb的值他就都能印出來...
我有試過改成
hql = "from OEB,OEA where oea.oea01=oeb01......
結果就無法了.....


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
LiaoLuke





發文: 106
積分: 0
於 2017-10-31 08:44 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
cool3690 wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.....
  com.tiptop.db.entity.OEA oea =null;
      hql = "from OEB where oea.oea01=oeb01 and to_char(oea.oea72, 'YYYY-MM-DD') between '" + sdf.format(beginDate) + "' and '" + sdf.format(endDate) + "' order by to_char(oea.oea72, 'YYYY-MM-DD')  desc \n"; 
       Iterator it;      
       
        com.tiptop.db.entity.OEB oeb ;
  java.util.List<com.tiptop.db.entity.OEB> oebList = new java.util.ArrayList<>();
   java.util.List<com.tiptop.db.entity.OEA> oeaList = new java.util.ArrayList<>();
  java.util.List list = this.hibernateTemplate.find(hql);    ==>這取得對映數據,取得 OEB對映 數據
  
        it=list.iterator();  
  while (it.hasNext())
  {
         oeb = (com.tiptop.db.entity.OEB) it.next();  ==>這取得數據,取得 OEB 數據,沒有取得OEA數據 
     oea61.add(new Double(oeb.getOeb13()).toString());
      oea72.add(oea.getOea61());  //改成oea就會抓不到值了   ==>這要取得 OEA 數據,當然會拿不到
  }

大大好,我hql沒問題耶?
只要都抓oeb的值他就都能印出來...
我有試過改成
hql = "from OEB,OEA where oea.oea01=oeb01......
結果就無法了.....


oea==>哪時?何時? 有數據? 您有瞭解 Hibernate 運作邏輯嗎?
//改成oea就會抓不到值了   ==>表示您不瞭解Hibernate 運作邏輯

Hibernate是一種Java語言下的物件關係對映解決方案。它是使用GNU寬通用公共許可證發行的自由、開源的軟體。
它為物件導向的領域模型到傳統的關係型資料庫的對映,提供了一個使用方便的框架

oea 跟oeb 有關聯,您在oeb.oea.......就會拿的到,
不過不知道您的觀念對不對?

不過您應該要知道, oeb. 可以看到是否有取到值


LiaoLuke edited on 2017-10-31 08:56
reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:LiaoLuke]
cool3690





發文: 44
積分: 0
於 2017-10-31 09:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大大好,
其實還不太了解 Hibernate ,努力學習中
想問那我有辦法把OEA import進來嗎?
(hql = "from OEB where oea.oea01=oeb01 ....
都可以抓oea比較了,無法比較完直接把值存下來嗎?
需要怎麼做呢?
還是我
public java.util.List<com.tiptop.db.entity.OEB> findOEBchart(....
就只能抓到oeb的值呢
感謝指教


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:snowbean71]
cool3690





發文: 44
積分: 0
於 2017-11-02 03:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
String sql =  "select a.oea01 from oea_file a,oeb_file b where a.oea01=b.oeb01 and to_char(a.oea72, 'YYYY-MM-DD') between '" + sdf.format(beginDate) + "' and '" + sdf.format(endDate) + "' order by to_char(a.oea72, 'YYYY-MM-DD')  desc \n"; 
        //-- 類別, 型態  
       list = this.getJdbcTemplate().queryForList(sql);
        it = list.iterator();
        if (it.hasNext()){
             oea = (com.tiptop.db.entity.OEA) it.next();
              oea72.add(oea.getOea61());
        }

大大好,
我試著這樣寫(不知道這方向對不對..

但她還是抓不到值耶?


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
snowbean71





發文: 92
積分: 0
於 2017-11-03 03:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你還是去看看hql 的寫法,我不知道你是你的兩個table 間是否有建立 CONTRACT 做外鍵索引,還是只是兩個table 中的column 以相同值去找。

這兩種方式對資料庫尋找時的花費時間差距很大,相同的對於沒有建外鍵索引的兩個table,在jpa 裏只能用原生sql 再轉為物件的做法。

也不知道你的entity class 是如何設定,不知道如何回你

下面這是之前寫的程式的部份的jpql ,這個jpql 將回傳 List<Rolename>

SELECT rr.rolename FROM Zacn ac JOIN ac.zrleList rr WHERE ac=:account


reply to postreply to post
javabean71.....snowbean71......JSF+JPA+Primefaces
作者 Re:JSF想抓2個資料表的值 [Re:snowbean71]
cool3690





發文: 44
積分: 0
於 2017-11-03 06:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

大大好:
上圖是我的table表,很單純的2個table沒做任何索引...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//OEA
public class OEA implements java.io.Serializable{
     String oea01;//單號
    String oea61;//金額
    String oea72;//date
  //setter,getter
}
 
//OEB
public class OEB implements Serializable{
 String oeb01;//單號
String oeb04;//客戶
 //setter,getter
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <class name="com.tiptop.db.entity.OEA" dynamic-insert="false" dynamic-update="false" mutable="true" 
           optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="oea_file">
<id column="oea01" name="oea01">
          <generator class="native"/>
        </id>
<property column="oea61" name="oea61"/>
          <property column="oea72" name="oea72"/>
</class>
 
-------------------------------
<class name="com.tiptop.db.entity.OEB" dynamic-insert="false" dynamic-update="false" mutable="true"  
           optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="oeb_file">
        <composite-id>
            <key-property name="oeb01" column="oeb01"/>
 </composite-id>
            <property column="oeb04" name="oeb04"/>
 
</class>
 

以上是entity class 請問這樣寫是OK的嗎?


reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:cool3690]
snowbean71





發文: 92
積分: 0
於 2017-11-04 05:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
網路找的,類似這個吧,自已研究
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Test
    public void testSql3()
    {
        Session session = null;
        try
        {
            session = HibernateUtil.openSession();
            /**
             * 当使用连接查询查询多个对象时,可以通过addEntity("alias", XXX.class)方法来根据
             * 数据库表的别名来引入多个实体类,这时如果需要将查询出来的所有的对象分别存入实体类中,
             * 只需要在查询出来的对象上添加 {} 号即可,此时就会自动帮我们分类
             */
            List<Object[]> stus = (List<Object[]>)session.createSQLQuery("select {stu.*}, {cla.*}, {spe.*}"
                    + " from t_student stu left join t_classroom cla on stu.rid=cla.id"
                    + " left join t_special spe on spe.id=cla.sid where stu.name like ?")
                                                    .addEntity("stu", Student.class)
                                                    .addEntity("cla", Classroom.class)
                                                    .addEntity("spe", Special.class)
                                                    .setFirstResult(0).setMaxResults(20)
                                                    .setParameter(0, "%张%")
                                                    .list();
            for(Object[] obj : stus)
            {
                Student stu = (Student)obj[0];
                Classroom cla = (Classroom)obj[1];
                Special spe = (Special)obj[2];
                System.out.println(stu.getName() + ", " + cla.getName() + ", " + spe.getName());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            HibernateUtil.close(session);
        }
    }


reply to postreply to post
javabean71.....snowbean71......JSF+JPA+Primefaces
作者 Re:JSF想抓2個資料表的值 [Re:snowbean71]
cool3690





發文: 44
積分: 0
於 2017-11-08 00:16 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好的,我會再試試看,謝謝

reply to postreply to post
作者 Re:JSF想抓2個資料表的值 [Re:snowbean71]
cool3690





發文: 44
積分: 0
於 2018-01-26 09:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
snowbean大大你好
我終於成功了,謝謝


cool3690 edited on 2018-01-28 02:49
reply to postreply to post
» JWorld@TW »  Web Framework » JSF

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8