使用JPA / Hibernate将我的实体绑定到具有@OneToMany批注

时间:2015-09-08 12:55:19

标签: java spring hibernate jpa

Spring的异常。当我尝试将我的实体绑定到具有@OneToMany注释的表时。

  

引起:org.hibernate.AnnotationException:@OneToOne或@ManyToOne   在com.wumf.iModuleSyncApps.impl.IAppEntity.packageName引用一个   未知实体:java.lang.String at   org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)     在   org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598)     在   org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1521)     在   org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)     在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:852)     ......还有41个

@Entity
@Table(name = "IApp")
public class IAppEntity {

    @ManyToOne
    @JoinColumn(referencedColumnName="packageName")
    @Id
    private String packageName;

    private String icon;

    private String name;

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Entity
@Table(name = "IAppsFriends")
public class IAppsFriends {

    @GeneratedValue
    @Id
    private int id;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "packageName")
    private List<IAppEntity> apps;

    private String phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<IAppEntity> getApps() {
        return apps;
    }

    public void setPackageName(List<IAppEntity> apps) {
        this.apps = apps;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

我也尝试将@JoinColumn(referencedColumnName="packageName")更改为 @JoinColumnn(name="packageName")无济于事。

2 个答案:

答案 0 :(得分:2)

您的 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/cardholderNameText" style="@style/form_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="@string/cardholder_name" /> </RelativeLayout> 注释放错了地方。当您应该注释@ManyToOne时,您正在注释String变量:

IAppsFriends

您还应该更改@Table(name = "IApp") public class IAppEntity { @Id private String packageName; @ManyToOne @JoinColumn(referencedColumnName="packageName") private IAppsFriends friends; // ... rest of code } 课程以修复IAppsFriends属性:

mappedBy

答案 1 :(得分:1)

如果要添加从IAppEntityIAppsFriends的OnwToMany关系,则应将IAppsFriends的ID存储在表IApp中。 IAppsFriends.id的类型为int。所以我认为你应该得到像friends int(11) not null这样的正确连接列。

然后JPA将依赖列朋友的值来获取实例IAppsFriends。

@Table(name = "IApp")
public class IAppEntity {

@Id
private String packageName;

@ManyToOne
@JoinColumn(referencedColumnName="id")
private IAppsFriends friends;

// ... rest of code

}

PS:referencedColumnName表示anther表中的引用。所以值应该是id(列朋友的值应该是IAppsFriends的id之一)

当然,IAppsFriends中的代码应修改为:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "friends")
private List<IAppEntity> apps;