开发者

JPA - manytoone cascading

I've got two entities

ServiceDownload.java

@Entity
public class ServiceDownload implements Serializable {
    private static final long serialVersionUID = -5336424090042137820L;

@Id
@GeneratedValue
private Long id;
@Length(max = 255)
private String description;

private byte[] download;

private String fileName;

@ManyToOne
private Service service;

Service.java

@Entity
public class Service implements Serializable {
    private static final long serialVersionUID = 4520872456865907866L;


@EmbeddedId
private ServiceId id;

@Length(max = 255)
private String servicename;

@Column(columnDefinition = "text")
private String highlightsText;
@Column(columnDef开发者_高级运维inition = "text")
private String detailsText;
@Column(columnDefinition = "text")
private String productText;
@Column(columnDefinition = "text")
private String dataText;

@ManyToMany(mappedBy = "services")
private Set<Machine> machines;

@OneToMany(targetEntity = ServiceDownload.class)
private List<ServiceDownload> serviceDownloads;

@OneToOne
private ServicePicture servicePicture;

When I create a new ServiceDownload Object and try to persists this I recieve a duplicate key exception. It seems that jpa tries to insert a new service object into the service table. How can I disable this behaviour?


You are using @GeneratedValue annotation for your @Id. According to JPA documentation, you should supply unique identifiers

By default, the application is responsible for supplying and setting entity identifiers (see @Id)

Try using a @SequenceGenerator and a sequence in your database to generate unique identifiers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜