簡體   English   中英

嘗試映射Three結構時,為數據庫加載所有元素時,在setter內發生Hibernate 4.3異常

[英]Hibernate 4.3 Exception occurred inside setter when load all element for the DB when try mapping a Three structure

運行查詢返回MessageUser類型的所有BD元素的查詢時,我陷入了休眠錯誤。

我正在映射二進制樹,MessageUser類包含此樹的副本。

我映射了帶有de對象子代和父對象的樹,並且將兩個子代與同一個父代映射,我想這是錯誤,但是我不知道有一種類似但可行的解決方案的替代方法。

這是我的代碼

抽象節點

/**
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package it.unibas.codinghuffman.model.logic.composit;

import it.unibas.codinghuffman.model.MessageUser;
import it.unibas.codinghuffman.model.logic.visitor.IVisitor;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Vincenzo Palazzo
 */
@Entity(name = "abstract_node")
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractNode implements INode, Comparable<AbstractNode> {

    private static final Log LOGGER = LogFactory.getLog(AbstractNode.class);

    protected String value;
    protected int frequency;
    protected int valueArch;
    private AbstractNode leaftNode;
    private AbstractNode rightNode;

    //for hibernate 
    private Long id;
    private MessageUser message;
    private AbstractNode father;

    public AbstractNode(AbstractNode leaftNode, AbstractNode rightNode) {

        this.leaftNode = leaftNode;
        this.rightNode = rightNode;

        this.leaftNode.setFather(this);
        this.leaftNode.setValueArch(0);

        this.rightNode.setFather(this);
        this.rightNode.setValueArch(1);

        if (this.leaftNode.getFather() == null) {
            throw new IllegalArgumentException("Fater null");
        }

        if (this.rightNode.getFather() == null) {
            throw new IllegalArgumentException("Fater null");
        }
    }

    public AbstractNode() {
    }

    @Transient
    public boolean isLeaft() {
        return leaftNode == null;
    }

    @OneToOne(mappedBy = "father", cascade = CascadeType.ALL)
    public AbstractNode getLeaftNode() {
        return leaftNode;
    }

    public void setLeaftNode(AbstractNode leaftNode) {
        this.leaftNode = leaftNode;
        this.leaftNode.setFather(this);
        this.leaftNode.setValueArch(0);
    }

    @OneToOne(mappedBy = "father", cascade = CascadeType.ALL)
    public AbstractNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(AbstractNode rightNode) {
        this.rightNode = rightNode;
        this.rightNode.setFather(this);
        this.rightNode.setValueArch(1);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
        return id;
    }

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

    @OneToOne
    public MessageUser getMessage() {
        return message;
    }

    public void setMessage(MessageUser message) {
        this.message = message;
    }

    @OneToOne
    public AbstractNode getFather() {
        return father;
    }

    public void setFather(AbstractNode father) {
        this.father = father;
    }

    public int getValueArch() {
        return valueArch;
    }

    public void setValueArch(int valueArch) {
        this.valueArch = valueArch;
    }

    public void accept(IVisitor visitor) {
        if (visitor == null) {
            LOGGER.error("The visitor into method accept is null");
            throw new IllegalArgumentException("The visitor into method accept is null");
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + Objects.hashCode(this.value);
        hash = 83 * hash + Objects.hashCode(this.leaftNode);
        hash = 83 * hash + Objects.hashCode(this.rightNode);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AbstractNode other = (AbstractNode) obj;
        if (!Objects.equals(this.value, other.value)) {
            return false;
        }
        if (!Objects.equals(this.leaftNode, other.leaftNode)) {
            return false;
        }
        if (!Objects.equals(this.rightNode, other.rightNode)) {
            return false;
        }
        return true;
    }

    @Override
    public int compareTo(AbstractNode o) {
        return ((Integer) frequency).compareTo(o.getFrequency());
    }
}

NodeComplex.java

/**
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package it.unibas.codinghuffman.model.logic.composit;

import it.unibas.codinghuffman.model.logic.visitor.IVisitor;
import javax.persistence.Entity;

/**
 *
 * @author Vincenzo Palazzo
 */
@Entity(name = "complex_node")
public class NodeComplex extends AbstractNode{

    public NodeComplex(String value, int frequency, AbstractNode leaftNode, AbstractNode rightNode) {
        super(leaftNode, rightNode);
        this.value = value;
        this.frequency = frequency;
    }

    public NodeComplex() {
        super();
        //throw new IllegalArgumentException("You are creating a lefth node, please you use the NodeLeaft implementation");
    }

    @Override
    public void accept(IVisitor visitor) {
        super.accept(visitor);

        visitor.visitComplexNode(this);
    }

}

NodeLeaft.class

/**
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package it.unibas.codinghuffman.model.logic.composit;

import it.unibas.codinghuffman.model.logic.visitor.IVisitor;
import javax.persistence.Entity;

/**
 *
 * @author Vincenzo Palazzo
 */
@Entity(name = "leafth_node")
public class NodeLeaft extends AbstractNode {

    public NodeLeaft(AbstractNode leaftNode, AbstractNode rightNode) {
        throw new IllegalArgumentException("You are creating a complex node, please you use the NodeComplex implementation");
    }

    public NodeLeaft(String value, int frequency) {
        super();
        this.value = value;
        this.frequency = frequency;
    }

    public NodeLeaft() {
        super();
    }

    @Override
    public void accept(IVisitor visitor) {
        super.accept(visitor);
        visitor.visitLeaftNode(this);
    }

    @Override
    public String toString() {
        return value + frequency;
    }



}

MessageUser.class

/*
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package it.unibas.codinghuffman.model;

import it.unibas.codinghuffman.model.logic.composit.AbstractNode;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 *
 * @author https://github.com/vincenzopalazzo
 */
@Entity(name = "messages")
public class MessageUser {

    private Long id;
    private String word;
    private int lenghtAsci;
    private int lenghtHuffman;
    private int ratioCompression;
    //This is not request to app
    private AbstractNode huffmanTree;
    private String huffmanCoding;
    private String stringHuffmanTree;

    public MessageUser() {}

    public MessageUser(String word, int lenghtAsci, int lenghtHuffman, int ratioCompression) {
        this.word = word;
        this.lenghtAsci = lenghtAsci;
        this.lenghtHuffman = lenghtHuffman;
        this.ratioCompression = ratioCompression;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
   // @Column(nullable = false)
    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    //@Column(nullable = false)
    public int getLenghtAsci() {
        return lenghtAsci;
    }

    public void setLenghtAsci(int lenghtAsci) {
        this.lenghtAsci = lenghtAsci;
    }

   // @Column(nullable = false)
    public int getLenghtHuffman() {
        return lenghtHuffman;
    }

    public void setLenghtHuffman(int lenghtHuffman) {
        this.lenghtHuffman = lenghtHuffman;
    }

   // @Column(nullable = false)
    public int getRatioCompression() {
        return ratioCompression;
    }

    public void setRatioCompression(int ratioCompression) {
        this.ratioCompression = ratioCompression;
    }

    @OneToOne(mappedBy = "message", cascade = CascadeType.ALL)
    public AbstractNode getHuffmanTree() {
        return huffmanTree;
    }

    public void setHuffmanTree(AbstractNode huffmanTree) {
        this.huffmanTree = huffmanTree;
        huffmanTree.setMessage(this);
    }

    public String getHuffmanCoding() {
        return huffmanCoding;
    }

    public void setHuffmanCoding(String huffmanCoding) {
        this.huffmanCoding = huffmanCoding;
    }

    public String getStringHuffmanTree() {
        return stringHuffmanTree;
    }

    public void setStringHuffmanTree(String stringHuffmanTree) {
        this.stringHuffmanTree = stringHuffmanTree;
    }
}

異常產生器

Caused by: org.hibernate.PropertyAccessException: Exception occurred inside setter of it.unibas.codinghuffman.model.logic.composit.AbstractNode.leaftNode
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:91)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:713)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:362)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4718)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:188)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:325)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2149)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
    at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:161)
    at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2385)
    at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:767)
    at org.hibernate.type.EntityType.resolve(EntityType.java:505)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:325)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2149)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
    at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:161)
    at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2385)
    at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:767)
    at org.hibernate.type.EntityType.resolve(EntityType.java:505)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1718)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at it.unibas.codinghuffman.persistence.hibernate.DAOGenericoHibernate.findByCriteria(DAOGenericoHibernate.java:105)
    ... 47 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
    ... 90 more
Caused by: java.lang.NullPointerException
    at it.unibas.codinghuffman.model.logic.composit.AbstractNode.setLeaftNode(AbstractNode.java:78)

我想給這個問題一個答案,因為休眠了兩個具有相同id的對象,所以從hibernate生成了異常,為了解決這個問題,但是我必須向配置的子級添加另一個信息,並且此配置是此JoinColum("fater_id")

孩子們的對應是這個

 @OneToOne(mappedBy = "father", cascade = CascadeType.ALL)
 @JoinColum("fater_id")
 public AbstractNode getLeaftNode() {
     return leaftNode;
 }

 @OneToOne(mappedBy = "father", cascade = CascadeType.ALL)
 @JoinColum("fater_id")
 public AbstractNode getRightNode() {
     return rightNode;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM