mlevytskiy
mlevytskiy

Reputation: 1675

Exception with JPA/Hibernate when binding my entities to tables with @OneToMany annotation

Exception with Spring. When I try to bind my entities to tables with @OneToMany annotation.

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.wumf.iModuleSyncApps.impl.IAppEntity.packageName references an unknown entity: java.lang.String at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109) at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598) at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1521) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) ... 41 more

@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;
    }
}

and

@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;
    }

}

Also I tried changing @JoinColumn(referencedColumnName="packageName") to @JoinColumnn(name="packageName") to no avail.

Upvotes: 0

Views: 618

Answers (2)

xierui
xierui

Reputation: 1055

If you want to add a OnwToMany relationship from IAppEntity to IAppsFriends, you should store the id of IAppsFriends in the table IApp. The type of IAppsFriends.id is int. So I think you should get a right join column like friends int(11) not null.

Then JPA will depend on the value of column friends to get the instance IAppsFriends.

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

@Id
private String packageName;

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

// ... rest of code

}

PS: referencedColumnName means the reference in anther table. So the value should be id (the value of column friends should be one of the id of IAppsFriends)

Of course the code in IAppsFriends should modified to:

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

Upvotes: 1

Tunaki
Tunaki

Reputation: 137269

Your @ManyToOne annotation is misplaced. You are annotating a String variable when you should be annotating a IAppsFriends:

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

    @Id
    private String packageName;

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

    // ... rest of code

}

You should also change IAppsFriends class to fix the mappedBy attribute:

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

Upvotes: 2

Related Questions