hibernateORM的三种方式

方式一:直接映射到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;

方式三:手动转换

你可能感兴趣的:(Hibernate)