- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我运行返回 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/
因此,我需要获取在为其赋值时调用的 setter 的名称。像这样: var b = {}; var a = { set hey(value) { b[] = value; } } 我希望 se
我是 Android 编程的新手(~ 2 个月)有必要为几十个不同的变量设置 getter 吗? 例如—— //Yes I realise that this isn't 'dozens' publi
import Control.Lens import Control.Lens.TH data Foo = Foo { _bar, _baz :: Int } makeLenses ''
我有点困惑:我可以覆盖 setter/getter 但仍然使用 super setter/getter 吗?如果是 - 怎么办? 用例: class A { void set value(num
我有一个接收消息的应用程序。消息中存在可编辑的字段。当字段更改时,应将其保存到数据库中。不幸的是,setter 仅在 setter 的范围内更改给定字段的值。知道为什么会发生这种情况吗?这是 gett
C# 中有没有一种方法可以让 setter 从“某物”继承,这样每次为特定基类及其继承者调用 setter 时,我都可以运行一些代码? 我想做的是在我的基类上有一个名为 IsValid 的 bool
可能是一个我无法解决的非常简单的问题 - 我从 C# 开始,需要使用 getter/setter 方法向数组添加值,例如: public partial class Form1 : Form {
这两个属性实现有什么区别? public override string A { get { return "s"; } set { } } public override strin
是否可以使用 abc.abstractproperty 创建一个具体的 getter 但将 setter 抽象为每个继承类的不同。我为每个子类处理不同的 val 设置。 例如。 @abstractpr
我在某处看到类似下面的内容,想知道它是什么意思。我知道他们是getter和setter,但是想知道为什么字符串Type是这样定义的。谢谢你帮助我。 public string Type { get;
Public class Example { private int number; public Example(int number){ this.number =
假设我有这样的代码: public response MyMethod(Request req) { String id = req.getFirst().geId(); } 我已经模拟了主对
允许这样做: public int Age { get; set; } 但是应用程序是否为变量创建/分配空间?我经常这样做 private int age = 0; public int Age {
我有一个条件,我构造字符串 (finalValue) 的方式是基于我在输入中获得的非空值的数量。所以我想知道是否可以用一个不同的参数为字符串 (finalValue) 重载 setter 方法并根据我
例如,在这段代码中 var o = { set a(value) {this.b = value}, get a() {return this.b} } 是否有可能获得对 o.a 的 sett
我一直在努力了解 getter 和 setter,但没有深入了解。我读过 JavaScript Getters and Setters和 Defining Getters and Setters只是没
我想在我的类中添加一个 getter 和 setter。然而,setter 应该接收一个 querySelector,但 getter 返回一个新类型 pageSections。 我的问题是 gett
使用有什么好处: private var _someProp:String; public function set someProp(value:String):void { _somePr
当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志! 有人对此
人员类别: public class Person { private String firstName; private String lastName; public Pe
我是一名优秀的程序员,十分优秀!