hibernate常见问题之解决

1.The database returned no natively generated identity value
表主键未设置AUTO_INCREMENT

 

2.com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 0 ms ago
严格说这不是hibernate的问题,而是数据库配置的问题,mysql的wait_timeout参数设置的问题。可以使用c3p0连接池来维持连接:
在hibernate.cfg.xml里增加:
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

 

3.一个对象ABean持有另一个对象BBean的引用,获取ABean对象的时候获取BBean。
public class ABean implements Serializable
{
 private BBean b;
 public BBean getB()
 {
  return reply;
 }

 public void setB(BBean b)
 {
  this.b = b;
 }
...
}
在ABean.hbm.xml的配置里如下写:
<many-to-one name="reply" class="org.csdn.BBean" fetch="join" unique="true" update="true" insert="true" optimistic-lock="true" not-found="exception">
 <column name="bId" />
</many-to-one>

 

4.有些hql无法执行,可以直接使用sql,例如:
public int[] getTotalAndRewaredCount()
{
 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 // 创建事务
 Transaction tx = session.beginTransaction();
 String hql = "SELECT count(id) FROM T_CUSTOMER union all select count(status) from T_CUSTOMER where rewardTime is not null";
 List lst = session.createSQLQuery(hql).list();
 Iterator it = lst.iterator();
 List<Integer> lstRes = new ArrayList<Integer>();
 while (it.hasNext())
 {
  int c = Integer.parseInt(it.next().toString());
  lstRes.add(c);
 }
 int s = lstRes.size();
 int[] arr = new int[s];
 for (int i = 0; i < s; i++)
 {
  arr[i] = lstRes.get(i);
 }
 tx.commit();
 return arr;
}

 

5.取表部分字段
方法1:在Bean里添加部分字段的构造器,在获取的时候:
 public List<MessageBean> retievePagedReplayedMessge(int start, int rows)
 {
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  Transaction tx = session.beginTransaction();
  Query query = session
    .createQuery("select new MessageBean(msgId,leaveName,leaveTitle,leaveTime) from MessageBean where replyId is not null order by msgId desc");
  query.setFirstResult(start);
  query.setMaxResults(rows);
  List<MessageBean> list = query.list();
  tx.commit();
  return list;
 }
方法2: public List<CustomerBean> retrieveManageCustomers()
 {
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  Transaction tx = session.beginTransaction();
  Criteria crit = session.createCriteria(CustomerBean.class);
  crit.setProjection(Projections.projectionList().add(
    Property.forName("registDate"), "registDate").add(
    Property.forName("customerSource"), "customersource").add(
    Property.forName("customerType"), "customertype").add(
    Property.forName("customerStatus"), "customerstatus").add(
    Property.forName("customerGrade"), "customergrade"));
  List<CustomerBean> list = crit.list();
  tx.commit();
  return list;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值