引起:org.hibernate.AnnotationException:@OneToOne或@ManyToOne on

时间:2016-04-05 11:30:06

标签: java hibernate jpa

我只是不明白这一点, 我正在尝试使用@OneToMany Mapping在这里使用hibernate实现设置java JPA并出现此错误

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.ppbni.splatter.model.KodePos.kode_korwil references an unknown entity: java.lang.String
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1550)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1473)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1389)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)

这是我的Kode_Pos类,

package org.ppbni.splatter.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.MapsId;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.ManyToOne;
import javax.persistence.CascadeType;

@SuppressWarnings("serial")
@Entity
@Table(name="kode_pos")
public class KodePos implements Serializable{

private String kode_pos;
private String kode_korwil;
private String kelurahan;
private String kecamatan;

@Id
@Column(name="kode_pos", unique=true, nullable=false)
public String getKode_pos() {
    return kode_pos;
}
public void setKode_pos(String kode_pos) {
    this.kode_pos = kode_pos;
}   
@Column(name="kelurahan")
public String getKelurahan() {
    return kelurahan;
}
public void setKelurahan(String kelurahan) {
    this.kelurahan = kelurahan;
}
@Column(name="kecamatan")
public String getKecamatan() {
    return kecamatan;
}
public void setKecamatan(String kecamatan) {
    this.kecamatan = kecamatan;
}

private Korwil korwil;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="kode_korwil")
public Korwil getKorwil() {
    return korwil;
}
public void setKorwil(Korwil korwil) {
    this.korwil = korwil;
}

/**
 * @param args
 */

}

这是我的korwil课程,

package org.ppbni.splatter.model;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.CascadeType;

@SuppressWarnings("serial")
@Entity
@Table(name="korwil")
public class Korwil implements Serializable{

private String kode_korwil;
private String no_dana;
private String nama_korwil;
private String alias_jabatan;
private String jabatan_korwil;

@Id
@Column(name="kode_korwil", unique=true, nullable=false)
public String getKode_korwil() {
    return kode_korwil;
}
public void setKode_korwil(String kode_korwil) {
    this.kode_korwil = kode_korwil;
}
@Id
@JoinColumn(name="no_dana")
public String getNo_dana() {
    return no_dana;
}
public void setNo_dana(String no_dana) {
    this.no_dana = no_dana;
}
@Column(name="nama_korwil")
public String getNama_korwil() {
    return nama_korwil;
}
public void setNama_korwil(String nama_korwil) {
    this.nama_korwil = nama_korwil;
}
@Column(name="alias_jabatan")
public String getAlias_jabatan() {
    return alias_jabatan;
}
public void setAlias_jabatan(String alias_jabatan) {
    this.alias_jabatan = alias_jabatan;
}
@Column(name="jabatan_korwil")
public String getJabatan_korwil() {
    return jabatan_korwil;
}
public void setJabatan_korwil(String jabatan_korwil) {
    this.jabatan_korwil = jabatan_korwil;
}

private Set<KodePos> kodepos;

@OneToMany(fetch = FetchType.LAZY, mappedBy="korwil")
public Set<KodePos> getKodepos() {
    return kodepos;
}
public void setKodepos(Set<KodePos> kodepos) {
    this.kodepos = kodepos;
}   

/**
 * @param args
 */

}

任何帮助都会很愉快:)

3 个答案:

答案 0 :(得分:2)

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="kode_korwil", nullable=false)
public Korwil getKode_korwil() {  //return type must be Korwil not String
    return kode_korwil;
}

我认为

@Id
@JoinColumn(name="no_dana") //Column instead JoinColumn ??
public String getNo_dana() {
    return no_dana;
}

同时删除其中一个@Id注释,因为您有2个Id注释,每个实体应该只有一个Id

@Id  <--------------------------------- remove one of @ID annotation.
@Column(name="kode_korwil", unique=true, nullable=false)
public String getKode_korwil() {
    return kode_korwil;
}

@Id  <--------------------------------- remove one of @ID annotation.
@JoinColumn(name="no_dana")
public String getNo_dana() {
    return no_dana;
}

答案 1 :(得分:1)

映射实体关系时,目标对象必须是实体。

在KodePos上试试

public class Korwil implements Serializable{

  private Korwil kode_korwil;
  ...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name="kode_korwil", nullable=false)
  public Korwil getKode_korwil() {
    return kode_korwil;
  }

我通常在字段上进行映射关系,所以也许我在属性映射上丢失了一些东西。

关于no_dana我认为Yusuf K.是对的。它必须是一列,如果它是关系中的一个字段,则必须使用映射和右类添加一个新属性。您必须避免更新和插入两者。只有一个属性应该是可更新的:

@Id
@Column(name="no_dana")
public String getId() {
...
@<rel_map_annot>
@JoinColumn(name="no_dana", insertable=false, updatable=false)
public NoDana getNoDana() {
...

希望它有所帮助!

答案 2 :(得分:0)

可能您应该忘记在hibernate.cfg.xml文件夹中添加“ Korwil”类。
所以必须如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/my_netflix?serverTimezone=Turkey</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">***</property>
        <property name="hibernate.connecti2on.pool_size">10</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>

        <property name="connection.autocommit">true</property>
        <property name="hbm2ddl.auto">update</property>

        <mapping class="org.ppbni.splatter.model.KodePos" />
        <mapping class="org.ppbni.splatter.model.Korwil" />

    </session-factory>
</hibernate-configuration>