javax.persistence.EntityExistsException:带有连接列的@ManyToOne

时间:2017-04-21 12:34:23

标签: spring hibernate jpa spring-boot

我尝试了不同的方法,但我无法弄清楚如何使这项工作。 我有两个实体: Waggon WaggonStation ,它们是n:1关系

@Entity
public class Waggon implements Serializable {

    @PrePersist
    protected void onCreate() {
        createdAt = new Date();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;

    private long waggonNr;
    private Date actDate;
    private boolean loaded;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = false)
    @JoinColumns({
                         @JoinColumn(name = "stationIm",
                                     referencedColumnName = "im", insertable = false,
                                     updatable = false),
                         @JoinColumn(name = "stationNumber",
                                     referencedColumnName = "number",
                                     insertable = false, updatable = false),
                         @JoinColumn(name = "stationName",
                                     referencedColumnName = "name",
                                     insertable = false, updatable = false)})
    private WaggonStation station;
    private Double[] coordinates;
    private boolean mockData;
}

 @Entity
 public class WaggonStation implements Serializable {
      public DasWaggonStation() {
      }

      public DasWaggonStation(int im, int number, String name) {
          this.im = im;
          this.number = number;
          this.name = name;
      }

      @Id private int im;
      @Id private int number;
      @Id private String name;
 }

当我试图坚持一个Waggons列表时

waggonRepo.save(waggonList); //CrudRepository<Waggon, Long>

我得到以下例外

  

javax.persistence.EntityExistsException:一个不同的对象   相同的标识符值已与会话关联:   [com.xx.xx.WaggonStation#com.xx.xx.WaggonStation@763568e4]

有人能指出我正确的方向吗?我错过了什么?

我尝试保存的列表包含这些元素(Waggon和WaggonStation都不包含任何元素)

{
  "number": "218124714435",
  "actDate": "2017-04-14T02:55:00Z",
  "status": "HandedOver",
  "loaded": false,
  "station": {
    "im": 55,
    "number": 16352,
    "name": "aName"
  },
  "coordinates": [
    14.6073,
    41.1969
  ]
},
{
  "number": "218124714435",
  "actDate": "2017-04-13T20:05:00Z",
  "status": "Arrival",
  "loaded": false,
  "station": {
    "im": 55,
    "number": 16352,
    "name": "aName"
  },
  "coordinates": [
    14.6073,
    41.1969
  ]
}

0 个答案:

没有答案