顯示具有 Hibernate 標籤的文章。 顯示所有文章
顯示具有 Hibernate 標籤的文章。 顯示所有文章

2012年6月28日 星期四

將Java的ArrayList(串列)寫到Mysql資料庫的blob欄位

案例: 收到的request資料中一個JSON格式的資料要保存,但為何不存JSON的字串就好?因為這個資料會不斷變動的,所以收到變動只要取出這個已將JSON轉成list的添加或刪除東西就好,不用再將JSON轉成list再轉回JSON存到DB去。 在Hibernate中是設定該欄位為type="java.sql.Blob",會自動在Mysql中建成DataType為LONGBLOB
下面是把JSON轉成list後,轉成blob型態存入DB
 
ObjectMapper mapper = new ObjectMapper();//用Jackson處理JSON
  Blob blob = null;
  try {
   SongResp dto = mapper.readValue(json, SongResp.class);
   List list = dto.getSongs();
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   ObjectOutputStream oos = new ObjectOutputStream(bout);
   oos.writeObject(list);
   bout.close();
   oos.close();
   byte[] asBytes = bout.toByteArray();
   blob = Hibernate.createBlob(asBytes);
   
  } catch (JsonParseException e) {
   e.printStackTrace();
  } catch (JsonMappingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

以下是把DB中的該欄位取出成當時寫入的list
          

List list;
  Blob b = userDao.getUserData(id, dataType);
  try {
   if (b != null) {
    InputStream in = b.getBinaryStream();

    ObjectInputStream ooi = new ObjectInputStream(in);

    in.close();
    ooi.close();

    list = (List)ooi.readObject();
    return list;
   }
    
  } catch (SQLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }

2011年11月29日 星期二

Default value between Hibernate and Mysql

Data type of Mysql is Integer

Hibernate :

   
   

 

Data type of Mysql is Varchar(1)

Hibernate :

    
    
  



Please focus on default=" 'a' " ! If you use default="a" , it will create failure on Hibernate and also Mysql !

2010年9月23日 星期四

用Hibernate取前幾筆資料

Using Hibernate to fetch top 10 high score

public List getSortedScoreList(){
return (List)this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
Criteria cri=session.createCriteria(ScoreVO.class);
cri.addOrder(Order.desc("score")).addOrder(Order.desc("createTime"));
cri.setMaxResults(10);
return cri.list();
}
});
}

2010年4月11日 星期日

TransactionException in Hibernate and MySQL

在過一段時間後會出現如上錯誤訊息,這是因為MySQL已經切斷閒置的連線,所以可以利用connection pool的配置來處理這個問題。
Mysql服務器預設的「wait_timeout」是8小時(待查證),所以mysql配置中my.ini 的wait_timeout值一定要大於等於connection pool的idle_timeout 值,否則mysql會在wait_timeout的時間後關閉連接,然而連接池還認為該連接可用,這樣就會產生以上圖內的錯誤。
MySQL :
interactive_timeout=XX
The number of seconds the server waits for activity on an interactive connection before closing it.

wait_timeout=XX
The number of seconds the server waits for activity on a noninteractive connection before closing it.

DBCP
timeBetweenEvictionRunsMillis 每毫秒檢查一次連接池中閒置的連接(spec :The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.)預設是-1

minEvictableIdleTimeMillis 連接池中連接可閒置的時間(spec:The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any). )

上面兩個設定要一起使用,意思是說每timeBetweenEvictionRunsMillis毫秒檢查一次連接池中空閒的連接,把空閒時間超過 minEvictableIdleTimeMillis毫秒的連接斷開,直到連接池中的連接數到minIdle為止

COVID-19 確診經歷紀實

原本以為真的是天選之人,就算先前家裡兩個小孩都確診都逃過了(可能有中獎但無症狀吧),不過就在2023年六月18日破解自認為天選之人的"心態",為什麼可以確認就是這天中獎的呢?因為在前都是居家上班,到人多的室內場所都會戴口罩,就剛好這天傍晚原本只想說要去附近的國...