关于hibernate 一对多级联查询的问题,生成的sql报语法错误

描述:主要是实体一对多的关系,不希望懒加载,需要急加载。配置完后,查询时报sql语法错误,将sql语句拷到mysql中执行,把{}去除,花括号其实就是映射的数据库字段,执行成功。仔细看了一下错误,象是hibernate执行时没有把花括号{}给替换成数据库字段名称,直接带着花括号查询了,当然会报错,为什么会出现这种问题,没有找到原因。有没有朋友知道的?
po类:一对多的关系,Jpa配置如下(单向关联)
[code="java"]
@SuppressWarnings("serial")
@Entity
@Table(name = "mplan_notice")
public class MplanNotice implements java.io.Serializable {
.....
private Set mplanNoticePositions = new HashSet(0);
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name = "{notice_id}")
@Cascade(value = { CascadeType.SAVE_UPDATE })
public Set getMplanNoticePositions() {
return mplanNoticePositions;
}

    public void setMplanNoticePositions(
            Set<MplanNoticePosition> mplanNoticePositions) {
        this.mplanNoticePositions = mplanNoticePositions;
    }

....

}
@SuppressWarnings("serial")
@Entity
@Table(name = "mplan_notice_position")
public class MplanNoticePosition implements java.io.Serializable {

    private String id;
    private Position position;
    private String noticeId;

    @Id
    @Column(name = "id" )
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    //定义外键名称
    @ManyToOne(cascade={CascadeType.PERSIST})
    @JoinColumn(name="{position_id}")
    public Position getPosition() {
        return position;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    @Column(name = "notice_id" )
    public String getNoticeId() {
        return noticeId;
    }

    public void setNoticeId(String noticeId) {
        this.noticeId = noticeId;
    }

}
[/code]
操作:采用hibernateTemplate.get(entityclass,id)获取MplanNotice对象,并且设置为fetch.eager,急查询,能直接把MplanNotice的Set集合获取。
错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not load an entity: [com.thinkwin.mlcm.mplan.po.MplanNotice#1]; SQL [select mplannotic0_.id as id38_10_, mplannotic0_.busstate as busstate38_10_, mplannotic0_.memo as memo38_10_, mplannotic0_.operate_time as operate4_38_10_, mplannotic0_.operate_user as operate8_38_10_, mplannotic0_.plan_month as plan5_38_10_, mplannotic0_.plan_quarter as plan6_38_10_, mplannotic0_.plan_year as plan7_38_10_, mplannotic0_.plantype as plantype38_10_, mplannotic1_.{notice_id} as column4_12_, mplannotic1_.id as id12_, mplannotic1_.id as id39_0_, mplannotic1[color=red]_.{job_id}[/color] as column3_39_0_, mplannotic1_.notice_id as notice2_39_0_, job2_.id as id71_1_, job2_.create_by as create2_71_1_, job2_.create_time as create3_71_1_, job2_.description as descript4_71_1_, job2_.job_name as job5_71_1_, job2_.org_id as org8_71_1_, job2_.position_id as position9_71_1_, job2_.update_by as update6_71_1_, job2_.update_time as update7_71_1_, mplannotic3_.{notice_id} as column4_13_, mplannotic3_.id as id13_, mplannotic3_.id as id40_2_, mplannotic3_.notice_id as notice2_40_2_, mplannotic3_.{person_id} as column3_40_2_, person4_.id as id73_3_, person4_.person_name as person2_73_3_, person4_.sex as sex73_3_, mplannotic5_.{notice_id} as column4_14_, mplannotic5_.id as id14_, mplannotic5_.id as id41_4_, mplannotic5_.notice_id as notice2_41_4_, mplannotic5_.{position_id} as column3_41_4_, position6_.position_id as position1_76_5_, position6_.create_by as create2_76_5_, position6_.create_time as create3_76_5_, position6_.description as descript4_76_5_, position6_.forder as forder76_5_, position6_.parent_position_id as parent9_76_5_, position6_.position_name as position6_76_5_, position6_.update_by as update7_76_5_, position6_.update_time as update8_76_5_, user7_.id as id87_6_, user7_.create_by as create2_87_6_, user7_.create_time as create3_87_6_, user7_.person_id as person9_87_6_, user7_.pwd as pwd87_6_, user7_.status as status87_6_, user7_.update_by as update6_87_6_, user7_.update_time as update7_87_6_, user7_.user_name as user8_87_6_, person8_.id as id73_7_, person8_.person_name as person2_73_7_, person8_.sex as sex73_7_, dictionary9_.DICT_ID as DICT1_94_8_, dictionary9_.DICT_NAME as DICT2_94_8_, dictionary9_.dict_type_id as dict5_94_8_, dictionary9_.DICT_CODE as DICT3_94_8_, dictionary9_.forder as forder94_8_, dictionary10_.DICT_TYPE_ID as DICT1_95_9_, dictionary10_.DICT_TYPE_CODE as DICT2_95_9_, dictionary10_.DICT_TYPE_NAME as DICT3_95_9_, dictionary10_.module_id as module5_95_9_, dictionary10_.state as state95_9_ from mplan_notice mplannotic0_ left outer join mplan_notice_job mplannotic1_ on mplannotic0_.id=mplannotic1_.{notice_id} left outer join org_job job2_ on mplannotic1_.{job_id}=job2_.id left outer join mplan_notice_person mplannotic3_ on mplannotic0_.id=mplannotic3_.{notice_id} left outer join org_person person4_ on mplannotic3_.{person_id}=person4_.id left outer join mplan_notice_position mplannotic5_ on mplannotic0_.id=mplannotic5_.{notice_id} left outer join org_position position6_ on mplannotic5_.{position_id}=position6_.position_id left outer join scy_user user7_ on mplannotic0_.operate_user=user7_.id left outer join org_person person8_ on user7_.person_id=person8_.id left outer join res_dictionary dictionary9_ on mplannotic0_.plantype=dictionary9_.DICT_ID left outer join res_dictionary_type dictionary10_ on dictionary9_.dict_type_id=dictionary10_.DICT_TYPE_ID where mplannotic0_.id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not load an entity: [com.thinkwin.mlcm.mplan.po.MplanNotice#1]

0
扫码支付0.1元 ×