Home JPA @ManyToOne Saving Issue
Reply: 0

JPA @ManyToOne Saving Issue

user5019
1#
user5019 Published in September 20, 2018, 12:45 pm

I have two entity called 'Group' and 'Generic'. When I tried to save Generic data in mySql with the reference of Group it save a new record to group table then use this record in Generic table. How to fix this problem???

Group Entity:

@Entity
@Table(name = "mdcn_group")
public class Group extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
protected Long id;

private String name;
private int status;
private String comments;

public Group() {
}

public Group(long id){
    this.id = id;
}

public Group(long id, String name) {
    this.id = id;
    this.name = name;
}
// ... getter and setter

Generic Entity:

@Entity
@Table(name = "generic")
public class Generic extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
protected Long id;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "group_id")
private Group group;
private String name;
private int status;
private String comments;

public Generic(){}

public Generic(long id){
    this.id = id;
}

public Generic(long id, String name){
    this.id = id;
    this.name = name;
}
// ... getter and setter
share|improve this question

3 Answers 3

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 1.002827 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO