Hibernate继承保存:saveOrUpdate和Merge生成极大数量的SELECT

时间:2011-10-31 14:39:15

标签: hibernate java-ee


我的类有以下结构。 超级班:

@EntityListeners(OnSaveListeners.class)
@Table(name = "DISPUNIT")
@Entity


@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class DistributedUnit  extends AbstractEntity {
    private static CompositeMetaData  metadata = (CompositeMetaData)MetaData.forName(DistributedUnit.class);

    @Transient
    public CompositeMetaData getMetaData(){
        return metadata;
    }

    public static final transient String FLD_UNIT_LEVEL = "unitLevel";

    private  DistributedUnitLevel unitLevel = DistributedUnitLevel.EXIT04;


    public void setUnitLevel(DistributedUnitLevel unitLevel){
            this.unitLevel = unitLevel;
    }

    public DistributedUnitLevel getUnitLevel(){
        return this.unitLevel;
    }

    public static final transient String FLD_CODE = "code";

    @Column(nullable = false)
    @NotNull
    private  String code;


    public void setCode(String code){
            this.code = code;
    }

    public String getCode(){
        return this.code;
    }

    public static final transient String FLD_NAME = "name";

    private  String name;


    public void setName(String name){
            this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public static final transient String FLD_TYPE = "type";

    private  UnitType type = UnitType.PUBLIC;


    public void setType(UnitType type){
            this.type = type;
    }

    public UnitType getType(){
        return this.type;
    }

    public static final transient String FLD_INSTALL_DATE = "installDate";

    @Temporal(TemporalType.TIMESTAMP)
    private  Date installDate;


    public void setInstallDate(Date installDate){
            this.installDate = installDate;
    }

    public Date getInstallDate(){
        return this.installDate;
    }

    public static final transient String FLD_NOTE = "note";

    private  String note;


    public void setNote(String note){
            this.note = note;
    }

    public String getNote(){
        return this.note;
    }

    public static final transient String FLD_REALTY = "realty";

    @ManyToOne(targetEntity = Realty.class, fetch = FetchType.LAZY)
    private  Realty realty;


    public void setRealty(Realty realty){
            this.realty = realty;
    }

    public Realty getRealty(){
        return this.realty;
    }

    public static final transient String FLD_CHILDREN = "children";

    @IndexColumn(base = 0, name = "idx_children_parent")
    @OneToMany(targetEntity = DistributedUnit.class, mappedBy = "parent")
    private  List<DistributedUnit> children = new ArrayList<DistributedUnit>();

    public void setChildren(List<DistributedUnit> children){

        List<DistributedUnit> preparedChildren = getChildren();
        if(preparedChildren != null ){
            preparedChildren.clear();
            preparedChildren.addAll(children);
        }else{
            this.children = children;
        }
    }

    public void addChildrenValue(DistributedUnit value){
        if(getChildren() == null) {
            setChildren( new ArrayList<DistributedUnit>()); 
        }
        getChildren().add(value);
        value.setParent(this);
    }

    public List<DistributedUnit> getChildren(){
        return this.children;
    }

    public static final transient String FLD_PARENT = "parent";

    @ManyToOne(targetEntity = DistributedUnit.class)
    private  DistributedUnit parent;


    public void setParent(DistributedUnit parent){
            this.parent = parent;
    }

    public DistributedUnit getParent(){
        return this.parent;
    }

    public static final transient String FLD_IDX_CHILDREN_PARENT = "idx_children_parent";

    private  Integer idx_children_parent;


    public void setIdx_children_parent(Integer idx_children_parent){
            this.idx_children_parent = idx_children_parent;
    }

    public Integer getIdx_children_parent(){
        return this.idx_children_parent;
    }


    /** Technical Fields **/

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    private String roleId;

    private Date creationDate ;

    private Date updateDate;

    @Version
    private int version ;

    private String creatorUser;

    private String updatorUser;

    private String departmentId;

    private String owner;

    private String ownerRole;


    public String getOwner() {
        return owner;
    }

    public String getOwnerRole() {
        return ownerRole;
    }

    public void setOwner(String owner) {
        this.owner = owner;

    }

    public void setOwnerRole(String ownerRole) {
        this.ownerRole = ownerRole;
    }

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

    public Long getId(){
        return this.id;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getCreatorUser() {
        return creatorUser;
    }

    public void setCreatorUser(String creatorUser) {
        this.creatorUser = creatorUser;
    }

    public String getUpdatorUser() {
        return updatorUser;
    }

    public void setUpdatorUser(String updatorUser) {
        this.updatorUser = updatorUser;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

}

----------------------------------------------- --------------------------------------------...

其中一个子类是:

@EntityListeners(OnSaveListeners.class)
@Entity
@PrimaryKeyJoinColumn(name="id")
public class TransStation extends DistributedUnit {
    private static CompositeMetaData  metadata = (CompositeMetaData)MetaData.forName(TransStation.class);

    @Transient
    public CompositeMetaData getMetaData(){
        return metadata;
    }



}

我使用hibernate,数据库是oracle。我有大量的数据&gt; 当我尝试编辑TransStation时,所以在第一次选择hibernate用来确定对象是新的还是已存在之后我得到了大量的SELECT命令

任何帮助

1 个答案:

答案 0 :(得分:0)

我没有仔细查看代码,但是这个

“..所以在第一次选择hibernate用来确定对象是新的还是存在之后我得到了极大量的SELECT命令..”

让我觉得这里存在某种n + 1问题。我不知道你在你的代码中做了什么,但我会尝试,为了更新实体,首先使用它的id从db加载它 - 所以Hibernate不必检查它是否已经存在或者没有,然后修改它的属性,然后保存。如果由于某种原因仍然看到大量的选择,请确保集合提取类型为LAZY(虽然我知道这个LAZY会在应用程序的其他部分产生n + 1个问题。看来Hibernate,至少是版本我使用它,不允许不同用例的不同获取模式..)

希望至少建议在哪里看..