gpt4 book ai didi

java - 当尝试映射三结构时加载数据库的所有元素时,Hibernate 4.3 setter 内部发生异常

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:19 25 4
gpt4 key购买 nike

当我运行返回 MessageUser 类型的所有 BD 元素的查询时,我遇到了 hibernate 错误。

我正在映射一棵二叉树,并且 MessageUser 类包含该树的副本。

我用 de 对象子对象和父对象映射了树,并且用同一个父亲映射了两个子对象,我想这是错误,但我不知道有一种类似但有效的解决方案的替代方法。

这是我的代码

AbstractNode.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.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);
}

}

NodeLeaf.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)

最佳答案

我想给这个问题一个答案,从hibernate生成的异常是因为hibernate fin两个具有相同id的对象而生成的,为了解决这个问题,但我必须向配置的子级添加另一个信息,这个配置是这个JoinColum("fater_id")

children 的映射是这样的

 @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;
}

关于java - 当尝试映射三结构时加载数据库的所有元素时,Hibernate 4.3 setter 内部发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56820189/

25 4 0
文章推荐: java - 如何防止 Element 变成 Element