| 1 | |
package jp.co.y_net.amm.dao; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | |
import jp.co.y_net.amm.common.AppUtils; |
| 6 | |
|
| 7 | |
import org.hibernate.Query; |
| 8 | |
import org.hibernate.SessionFactory; |
| 9 | |
import org.hibernate.classic.Session; |
| 10 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 11 | |
import org.springframework.beans.factory.annotation.Qualifier; |
| 12 | |
import org.springframework.orm.hibernate3.HibernateTemplate; |
| 13 | |
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
| 14 | |
import org.springframework.stereotype.Component; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
@Component("abstractDao") |
| 22 | 0 | abstract class AbstractDao extends HibernateDaoSupport { |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
@Autowired(required = true) |
| 31 | |
@Qualifier("sessionFactory_Amm") |
| 32 | |
public void init(SessionFactory sessionFactory){ |
| 33 | 0 | super.setSessionFactory(sessionFactory); |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
protected void add(AppDto item, Integer updateusrid) { |
| 41 | 0 | if(item.getId() != null) { |
| 42 | 0 | throw new RuntimeException("追加対象のオブジェクトに主キーが設定されています。"); |
| 43 | |
} |
| 44 | |
|
| 45 | |
|
| 46 | 0 | if(item != null) { |
| 47 | 0 | item.setUpdatedate(AppUtils.createNowLong()); |
| 48 | 0 | item.setUpdateusrid(updateusrid); |
| 49 | 0 | item.setDeleted(AppDef.FLASE); |
| 50 | |
} |
| 51 | |
|
| 52 | |
try { |
| 53 | 0 | HibernateTemplate hibernateTemplate = getHibernateTemplate(); |
| 54 | 0 | hibernateTemplate.save(item); |
| 55 | 0 | hibernateTemplate.flush(); |
| 56 | 0 | } catch (Exception e) { |
| 57 | 0 | throw new RuntimeException(e); |
| 58 | |
} |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
protected void update(AppDto item, Integer updateusrid) { |
| 66 | 0 | if(item.getId() == null) { |
| 67 | 0 | throw new RuntimeException("追加対象のオブジェクトに主キーが設定されていません。"); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | 0 | if(item != null) { |
| 72 | 0 | item.setUpdatedate(AppUtils.createNowLong()); |
| 73 | 0 | item.setUpdateusrid(updateusrid); |
| 74 | |
} |
| 75 | |
|
| 76 | |
try { |
| 77 | 0 | HibernateTemplate hibernateTemplate = getHibernateTemplate(); |
| 78 | |
|
| 79 | 0 | hibernateTemplate.update(item); |
| 80 | 0 | hibernateTemplate.flush(); |
| 81 | |
|
| 82 | 0 | } catch (Exception e) { |
| 83 | 0 | throw new RuntimeException(e); |
| 84 | |
} |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected void removeLogical(AppDto item, Integer updateusrid) { |
| 92 | 0 | item.setDeleted(AppDef.TRUE); |
| 93 | 0 | this.update(item, updateusrid); |
| 94 | 0 | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
protected void remove(AppDto item) { |
| 99 | 0 | throw new RuntimeException("本アプリケーションでは物理削除を行わない。"); |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public Integer getNextId(String entityname, String colname) { |
| 109 | 0 | return getNextId(entityname, colname, 1); |
| 110 | |
} |
| 111 | 0 | public Integer getNextId(String entityname, String colname, int init) { |
| 112 | 0 | Session session = getSessionFactory().openSession(); |
| 113 | 0 | Query query = session.createQuery("SELECT max(" + colname + ") from " + entityname + ""); |
| 114 | 0 | @SuppressWarnings("rawtypes") |
| 115 | 0 | List list = query.list(); |
| 116 | 0 | Integer currentId = 0; |
| 117 | 0 | Integer nextId; |
| 118 | 0 | if(list != null && list.isEmpty() == false && (Integer)list.get(0) != null) { |
| 119 | 0 | currentId = (Integer)list.get(0); |
| 120 | 0 | nextId = currentId + 1; |
| 121 | 0 | } else { |
| 122 | 0 | nextId = init; |
| 123 | |
} |
| 124 | 0 | session.close(); |
| 125 | 0 | return nextId; |
| 126 | |
} |
| 127 | |
|
| 128 | |
} |