hibernateORM的三种方式
来源:互联网 发布:java list entity 编辑:程序博客网 时间:2024/05/16 01:47
方式一:直接映射到Class
首先,在hbm文件(如果是spring,则是ApplicationContext.xml)中添加Class的bean
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hiberante.format_sql">true</prop> <prop key="hiberante.connection.autocommit">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> </props> </property> <property name="annotatedClasses"> <list> <value>cn.paic.rep.pare.modal.EbdUserAccountEntity</value> <value>cn.paic.rep.pare.modal.EbdGbdEtrprsNewsEntity</value> <value>cn.paic.rep.pare.modal.EbdModlFocusCompanyEntity</value> <value>cn.paic.rep.pare.modal.EbdDailyNewsAndCompanyEntity</value> <value>cn.paic.rep.pare.modal.EbdCompanyHoldingLotsEntity</value> <value>cn.paic.rep.pare.modal.EbdModlCompanyNameEntity</value> <value>cn.paic.rep.pare.modal.EbdUserGroupEntity</value> <value>cn.paic.rep.pare.modal.EbdGroupIndustryEntity</value> <value>cn.paic.rep.pare.modal.EbdAuthEmailSendRecordEntity</value> <value>cn.paic.rep.pare.modal.EbdEtrprsSignalEntity</value> <value>cn.paic.rep.pare.modal.EbdModlEtrprsSignalEntity</value> <value>cn.paic.rep.pare.modal.EbdSignalBasicInfoEntity</value> </list> </property> </bean>
然后,需要在类上注解方式映射到表
@Entity@Table(name = "ebd_auth_group_industry")public class EbdGroupIndustryEntity { private String industryCode; private Long groupId; private String industryType; @Id @Column(name = "industry_code") public String getIndustryCode() { return industryCode; } public void setIndustryCode(String industryCode) { this.industryCode = industryCode; } @Id @Column(name = "group_id") public Long getGroupId() { return groupId; } public void setGroupId(Long groupId) { this.groupId = groupId; } @Id @Column(name = "industry_type") public String getIndustryType() { return industryType; } public void setIndustryType(String industryType) { this.industryType = industryType; }}
List<EbdGroupIndustryEntity> ret; String sql = "select industry_type, industry_code, group_id " + " from ebd_auth_group_industry " + " where group_id="+groupId+" and industry_type='"+industryType+"'"; Session session = getSession(); SQLQuery q = session.createSQLQuery(sql); q.addEntity(EbdGroupIndustryEntity.class); ret = q.list();
方式二:无主键表,转换到Class
List<BlackListEntity> ret; String sql = " select company_id as companyId," + " company_full_name as companyFullName," + " company_short_name as companyShortName" + " from ebd_exclude_company_detail " + " where company_source='1'";//白名单company_source='1' Session session = getSession(); Query query = session.createSQLQuery(sql) .addScalar("companyId", StandardBasicTypes.INTEGER) .addScalar("companyFullName", StandardBasicTypes.STRING) .addScalar("companyShortName", StandardBasicTypes.STRING) .setResultTransformer(Transformers.aliasToBean(BlackListEntity.class)); ret = query.list(); return ret;
方式三:手动转换
0 0
- hibernateORM的三种方式
- 数据库连接的三种方式
- 数据库连接的三种方式..
- swap的三种方式
- insertFeatureClass的三种方式
- onclick的三种方式
- OnClick的三种方式
- 定位的三种方式
- GC的三种方式
- GC的三种方式
- 多线程的三种方式
- css的三种方式
- insertFeatureClass的三种方式
- 代理的三种方式
- HibernateORM操作类..
- HibernateOrm元素配置
- List的遍历的三种方式
- .net的三种方式的事务处理
- No system information found in LMDB, can't find Technical system after RZ70
- dubbo+zookeeper+dubbo管理控制台实践demo
- 初识MVVM的心得
- HDU 1257 最少拦截系统(贪心)
- 而立之年——三线城市程序员的年终告白
- hibernateORM的三种方式
- linux-vyos-ebtables 解决国内慢和无法访问的问题,(原因是CT这边的路由器开启了arp绑定和防欺骗功能)
- Scala学习整理[第三十章 Actor和并发]<Programming In Scala>
- 不良资产处置中风险度量存在的问题
- 配置openresty的一些坑
- SegmentFault 技术周刊 Vol.21 - 程序人生(二):2016 这一年
- java 非静态内部类与外部类引用之间的关系
- 如何使处于不同局域网的计算机实现远程通信
- POJ 1001 Exponentiation 高精度小数相乘