简体   繁体   中英

JPA OneToMany MappedBy relationships

I have three classes, Site, GoupIP and IP

A Site has one or many GrouIPs. A GroupIP has one or many IPs.

Here is the code:

Site

@Entity
@Table(name = "site")
public class Site implements Serializable {
private Set<GroupIp> groups;

@OneToMany(mappedBy = "site", fetch = FetchType.EAGER, cascade =CascadeType.ALL)
public Set<GroupIp> getGroups() {
    return groups;
}

public void setGroups(Set<GroupIp> groups) {
    this.groups = groups;
}

}

GroupIP

@Entity
@Table(name = "groupip")
public class GroupIp implements Serializable {
private Set<Ip> ips;
private Site site;

@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "site_id")
public Site getSite() {
return site;
}

@OneToMany(mappedBy = "groupip", fetch = FetchType.EAGER, cascade =CascadeType.ALL)
public Set<Ip> getIps() {
    return ips;
}

public void setIps(Set<Ip> ips) {
    this.ips= ips;
}

}

IP

@Entity
@Table(name = "ip")
public class Ip implements Serializable {
private GroupIp groupIp;

@ManyToOne(targetEntity = GroupIp.class,cascade = CascadeType.MERGE)
@JoinColumn(name = "groupip_id", nullable=false)
public GroupIp getGroupIp() {
return groupIp;
}

public void setGroupIp(GroupIp groupIp) {
        this.groupIp = groupIp;
}

}

On GroupIp class, I m getting:

In attribute 'ips', the "mapped by" value 'groupip' cannot be resolved to an attribute on the target entity.

Whats wrong on my code ??

The mappedBy name that you have to put in the relationship is the name of the class attribute, not the table name.

So put @OneToMany(mappedBy = "groupIp",... (note the uppercase) instead of @OneToMany(mappedBy = "groupip",...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM