我有这两个班 - Letadlo和Letiste(飞机和(家)机场)。在使用java和hibernate将数据保存到数据库时,我得到了这个例外:
16285 [Thread-2] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: mapy.Letiste, getter method of property: id
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of mapy.Letiste.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:198)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:227)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3876)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3584)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:159)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:91)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:310)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
at logika.UkladaniDoDtb.run(UkladaniDoDtb.java:34)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
... 19 more
保存部分:
HibernateFactory.buildSessionFactory();
Session session = HibernateFactory.openSession();
session.beginTransaction().begin();
for(Object o: DocasneUloziste.objektyKeSmazani){
session.delete(o);
}
for (Linka l : DocasneUloziste.linky) {
session.saveOrUpdate(l);
}
for (Letadlo l : DocasneUloziste.letadla) { //this is 34 line
session.saveOrUpdate(l);
}
for (Letiste d : DocasneUloziste.seznamLetist) {
session.saveOrUpdate(d);
}
session.getTransaction().commit();
session.close();
Letadlo的xml:
<class name="mapy.Letiste">
<meta attribute="class-description">Tato trida obsahuje info o domovskem letisti</meta>
<id name="id" type="long" column="LETISTE_ID">
<generator class="native" />
</id>
<property name="kodLetiste" type="string" not-null="true" length="3" column="KOD_LETISTE"/>
<property name="mesto" type="string" not-null="true" length="35" column="MESTO" />
<set name="obsluhaLetadel" cascade="all">
<key column="LETISTE_ID" />
<one-to-many class="mapy.Letadlo" />
</set>
</class>
Letml的xml:
<class name="mapy.Letiste">
<meta attribute="class-description">Tato trida obsahuje info o domovskem letisti</meta>
<id name="id" type="long" column="LETISTE_ID">
<generator class="native" />
</id>
<property name="kodLetiste" type="string" not-null="true" length="3" column="KOD_LETISTE"/>
<property name="mesto" type="string" not-null="true" length="35" column="MESTO" />
<set name="obsluhaLetadel" cascade="all">
<key column="LETISTE_ID" />
<one-to-many class="mapy.Letadlo" />
</set>
</class>
letadlo类包含我在XML中使用的getter和setter的所有变量以及没有参数的构造函数。
感谢您的任何建议!
编辑:Letiste Class
public class Letiste {
private Long id;
private String kodLetiste;
private String mesto;
private Set<Letadlo> obsluhaLetadel = new HashSet<Letadlo>();
public Letiste() {
}
public Letiste(String kodLetiste, String mesto) {
this.kodLetiste = kodLetiste;
this.mesto = mesto;
}
public String getKodLetiste() {
return kodLetiste;
}
public void setKodLetiste(String kodLetiste) {
this.kodLetiste = kodLetiste;
}
public String getMesto() {
return mesto;
}
public void setMesto(String mesto) {
this.mesto = mesto;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Letiste other = (Letiste) obj;
if ((this.kodLetiste == null) ? (other.kodLetiste != null) : !this.kodLetiste.equals(other.kodLetiste)) {
return false;
}
if ((this.mesto == null) ? (other.mesto != null) : !this.mesto.equals(other.mesto)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 13 * hash + (this.kodLetiste != null ? this.kodLetiste.hashCode() : 0);
hash = 13 * hash + (this.mesto != null ? this.mesto.hashCode() : 0);
return hash;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<Letadlo> getObsluhaLetadel() {
return obsluhaLetadel;
}
public void setObsluhaLetadel(Set<Letadlo> obsluhaLetadel) {
this.obsluhaLetadel = obsluhaLetadel;
}
编辑:Letadlo Class
public class Letadlo {
private Long id;
private String kodLetadla;
private String kapacita;
private Integer dolet;
private Set<Linka> seznamLinek = new HashSet<Linka>();
private String letiste;
public Letadlo() {
}
public Letadlo(String kodLetadla, String kapacita , Integer dolet, String letiste) {
this.kodLetadla = kodLetadla;
this.kapacita = kapacita;
this.dolet = dolet;
this.letiste = letiste;
}
/**
* @return the kodLetadla
*/
public String getKodLetadla() {
return kodLetadla;
}
/**
* @param kodLetadla the kodLetadla to set
*/
public void setKodLetadla(String kodLetadla) {
this.kodLetadla = kodLetadla;
}
/**
* @return the kapacita
*/
public String getKapacita() {
return kapacita;
}
/**
* @param kapacita the kapacita to set
*/
public void setKapacita(String kapacita) {
this.kapacita = kapacita;
}
/**
* @return the dolet
*/
public Integer getDolet() {
return dolet;
}
/**
* @param dolet the dolet to set
*/
public void setDolet(Integer dolet) {
this.dolet = dolet;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Letadlo other = (Letadlo) obj;
if (this.kodLetadla != other.kodLetadla && (this.kodLetadla == null || !this.kodLetadla.equals(other.kodLetadla))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (this.kodLetadla != null ? this.kodLetadla.hashCode() : 0);
hash = 79 * hash + (this.kapacita != null ? this.kapacita.hashCode() : 0);
hash = 79 * hash + (this.dolet != null ? this.dolet.hashCode() : 0);
return hash;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the seznamLinek
*/
public Set<Linka> getSeznamLinek() {
return seznamLinek;
}
/**
* @param seznamLinek the seznamLinek to set
*/
public void setSeznamLinek(Set<Linka> seznamLinek) {
this.seznamLinek = seznamLinek;
}
/**
* @return the letiste
*/
public String getLetiste() {
return letiste;
}
/**
* @param letiste the letiste to set
*/
public void setLetiste(String letiste) {
this.letiste = letiste;
}