当我试图获得实体属性时,我遇到了奇怪的问题:
Hibernate:
select
tarifklsk0_.ID as ID1_12_0_,
tarifklsk0_.FK_TARIF as FK_TARIF2_12_0_,
tarifservp1_.FK_TARIF as FK_TARIF2_11_1_,
tarifservp1_.ID as ID1_11_1_,
tarifservp1_.ID as ID1_11_2_,
tarifservp1_.FK_TARIF as FK_TARIF2_11_2_,
tarifservp1_.N1 as N3_11_2_
from
TR.TARIFXKLSK tarifklsk0_
left outer join
TR.TARIF_SERV_PROP tarifservp1_
on tarifklsk0_.FK_TARIF=tarifservp1_.FK_TARIF
where
tarifklsk0_.ID=?
Jun 13, 2016 7:38:26 AM org.hibernate.event.internal.DefaultLoadEventListener doOnLoad
INFO: HHH000327: Error performing load command : org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Integer TarifKlsk.fkTarif] by reflection for persistent property [TarifKlsk#fkTarif] : 1027303
Exception in thread "main" org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Integer TarifKlsk.fkTarif] by reflection for persistent property [TarifKlsk#fkTarif] : 1027303
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
....skipped...
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field TarifKlsk.fkTarif to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
我的实体:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.BatchSize;
@SuppressWarnings("serial")
@Entity
@Table(name = "TARIFXKLSK", schema="TR")
public class TarifKlsk implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private Integer id;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name="FK_TARIF", referencedColumnName="FK_TARIF")
@BatchSize(size = 50)
private Set<TarifServProp> tarifservprop = new HashSet<TarifServProp>(0);
@Column(name = "FK_TARIF", updatable = false, nullable = false)
private Integer fkTarif;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<TarifServProp> getTarifservprop() {
return tarifservprop;
}
public void setTarifservprop(Set<TarifServProp> tarifservprop) {
this.tarifservprop = tarifservprop;
}
public Integer getFkTarif() {
return fkTarif;
}
public void setFkTarif(Integer fkTarif) {
this.fkTarif = fkTarif;
}
}
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "TARIF_SERV_PROP", schema="TR")
public class TarifServProp implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", updatable = false, nullable = false)
private Integer id;
@Column(name = "FK_TARIF", updatable = false, nullable = false)
private Integer fkTarif;
public Integer getId() {
return id;
}
@Column(name = "N1", updatable = false, nullable = false)
private Integer n1;
public void setId(Integer id) {
this.id = id;
}
public Integer getFkTarif() {
return fkTarif;
}
public void setFkTarif(Integer fkTarif) {
this.fkTarif = fkTarif;
}
public Integer getN1() {
return n1;
}
public void setN1(Integer n1) {
this.n1 = n1;
}
}
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
TarifKlsk k2=sess.get(TarifKlsk.class, 1027303);
for (TarifServProp t : k2.getTarifservprop()) {
System.out.println("Tar="+t.getN1());
}
System.out.println("End init");
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journaldev.hibernate</groupId>
<artifactId>HibernateEHCacheExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Hibernate Secondary Level Cache Example using EHCache implementation</description>
<dependencies>
<!-- Hibernate Core API -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- EHCache Core APIs -->
<!-- http://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- EHCache uses slf4j for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
它是5.0和5.1.0版本https://hibernate.atlassian.net/browse/hhh-10618中的bug
用Mysql,intellij作为IDE在Spring启动时使用JPA。 当我尝试保存游戏实体时,我会遇到以下错误,但如果保存玩家实体,则所有操作都很正常 谢谢
我有一个非常简单的Hibernate设置,但我收到这个奇怪的错误时,保存一个实体: org.hibernate.property.access.spi.属性访问字段[私有java.lang.字符串com.example.demo.Student.first名称]通过反射为持久属性[com.example.demo.学生#firstName]:学生[id=0, firstName=My, lastN
/实体类/包实体的代码; 完整堆栈跟踪是
问题内容: 我找到了一种通过来获取继承成员并通过来获得 ; 私有成员的方法,但是我正在寻找私有的继承字段。我怎样才能做到这一点? 问题答案: 这应该演示如何解决它: (或Class.getDeclaredFields用于所有字段的数组。) 输出:
persistenceException:DB2 SQL错误:sqlcode=-206,sqlstate=42703,sqlerrmc=t0.id,driver=3.52.95{prepstmnt 1029586270
问题内容: 考虑这个例子: 允许您通过反射来访问类的私有字段似乎是不合逻辑的。为什么有这样的功能?允许这样的访问不是“危险”吗? 问题答案: 专用旨在防止意外滥用,而不是作为一种安全机制。如果您选择绕过它,那么您可以自行承担风险,并假设您知道自己在做什么。