I spent a lot of time looking for the error, and I cannot solve this problem. I am using spring JPA with Hibernate and Postgre. I have two Location and Ap objects:
CREATE TABLE ap ( id serial NOT NULL, ssid text NOT NULL, bssid text NOT NULL, capabilities text, CONSTRAINT ap_pkey PRIMARY KEY (id ) ) CREATE TABLE location2 ( latitude double precision NOT NULL, longitude double precision NOT NULL, power integer NOT NULL, ap_id integer, id serial NOT NULL, CONSTRAINT location2_pkey PRIMARY KEY (id ), CONSTRAINT location2_ap_id_fkey FOREIGN KEY (ap_id) REFERENCES ap (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION )
Ar:
import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "ap") public class Ap implements Serializable { private Long id; private String ssid; private String bssid; private String capabilities; private Set<Location> locations = new HashSet<Location>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column public Long getId() { return id; } public void setId(Long id) { this.id = id; } @OneToMany(mappedBy = "ap", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) public Set<Location> getLocations() { return locations; } public void setLocations(Set<Location> locations) { this.locations = locations; } @Column public String getSsid() { return ssid; } public void setSsid(String ssid) { this.ssid = ssid; } @Column public String getBssid() { return bssid; } public void setBssid(String bssid) { this.bssid = bssid; } @Column public String getCapabilities() { return capabilities; } public void setCapabilities(String capabilities) { this.capabilities = capabilities; } public void addLocation(Location location) { location.setAp(this); getLocations().add(location); } public void removeLocation(Location location) { getLocations().remove(location); } public Ap() { super(); } public Ap(String ssid, String bssid, String capabilities) { this.ssid = ssid; this.bssid = bssid; this.capabilities = capabilities; } }
LOCATION2
@Entity @Table(name = "location2") public class Location implements Serializable { private Long id; private double latitude; private double longitude; private int power; private Ap ap; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne @JoinColumn(name = "ap_id") public Ap getAp() { return ap; } public void setAp(Ap ap) { this.ap = ap; } @Column public double getLatitude() { return latitude; } public void setLatitude(double latitude) { this.latitude = latitude; } @Column public double getLongitude() { return longitude; } public void setLongitude(double longitude) { this.longitude = longitude; } @Column public int getPower() { return power; } public void setPower(int power) { this.power = power; } public Location(double latitude, double longitude, int power) { super(); this.latitude = latitude; this.longitude = longitude; this.power = power; } public Location() { super(); }
When I add one location to Ap, it works, but when Ap has two or more locations, I cannot save the Ap object.
ApService service=context.getBean("ApService", ApService.class); Ap ap=new Ap("test_ssid2", "test_bssid2", "test_capabilities2"); ap.addLocation(new Location(9.121, 12.9, 12)); ap.addLocation(new Location(9.122, 12.9, 12)); service.save(ap);
ApService:
public Ap save(Ap ap) { return apRepository.save(ap); }
In this case, an error occurs:
Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: a different object with the same identifier value was already associated with the session: [com.kulig.ap.domain.Location
I also tried EntityManager with merge and persist methods and this gives the same error. Please, help.