我已经编码了三个类(User
,UserCredential
,Address
),我想在其中使用映射将数据存储到表中。我正在使用JSON将数据存储到表中。
当我存储数据时,数据存储在所有表中,但是在用户ID中,它显示1
,在UserCredential ID中,它显示3
,在地址ID中,它显示2
条目的所有ID应该为1
。
类是
package com.spring.demo.model;
import java.util.Date;
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.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="user")
public class User {
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String fName;
private String lName;
@Column(unique=true,nullable=true)
private String email;
@Column(unique=true,nullable=true)
private long mobile;
private Date dob;
@Lob
private byte[] image;
@Transient
private String base64Image;
@OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
@JoinColumn(name="userCredential_id")
private UserCredential userCredential;
@OneToOne(cascade=CascadeType.ALL,fetch =FetchType.EAGER)
@JoinColumn(name="add_id")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public UserCredential getUserCredential() {
return userCredential;
}
public void setUserCredential(UserCredential userCredential) {
this.userCreenter code heredential = userCredential;
}
}
UserCredential.java
package com.spring.demo.model;
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.JoinColumn;
import javax.persistence.OneToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class UserCredential {
@Id
@Column(name="credential_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(unique=true,nullable=true)
private String username;
private String password;
private String cnfrmpassword;
@JsonIgnore
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="user_id")
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCnfrmpassword() {
return cnfrmpassword;
}
public void setCnfrmpassword(String cnfrmpassword) {
this.cnfrmpassword = cnfrmpassword;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Address.java
package com.spring.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="address")
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="add_id")
private int id;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="house_no")
private String h_no;
@ManyToOne
@JoinColumn(name="user_id", nullable=true)
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getH_no() {
return h_no;
}
public void setH_no(String h_no) {
this.h_no = h_no;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
我真的不明白如何克服这个问题。
JSON
格式以存储数据
{
"fName":"suresh kumst",
"lName":"dingh",
"mobile":4595498366,
"email":"ksuraj1sd00@gmail.com",
"dob":"2012-04-23T18:25:43.511Z",
"address":{
"city":"noida",
"state":"up",
"h_no":"123"
},
"userCredential":{
"username":"ksuraj1asd002",
"password":"12345",
"cnfrmpassword":"12345"
}
}
以及具有不同id的响应,尽管它们在第一项中应为1,并且用户id应具有值
{
"id": 1,
"fName": "suresh kumst",
"lName": "dingh",
"email": "ksuraj1sd00@gmail.com",
"mobile": 4595498366,
"dob": "2012-04-23T18:25:43.511+0000",
"image": null,
"userCredential": {
"id": 3,
"username": "ksuraj1asd002",
"password": "12345",
"cnfrmpassword": "12345"
},
"address": {
"id": 2,
"city": "noida",
"state": "up",
"h_no": "123",
"user": null
}
}
答案 0 :(得分:1)
在这里,您正在使用@GeneratedValue(strategy=GenerationType.AUTO)
。
一些数据库将使用公共序列来生成并分配序列号。 Hibernate将创建一个表hibernate_sequence
,所有实体表都将引用该表以获取下一个序列号。因此,主键将分散在实体之间。
要使每个实体的主键都从1开始,请对每个实体使用@GeneratedValue(strategy=GenerationType.IDENTITY)
。
答案 1 :(得分:0)
请使用数据库序列,以便您更好地控制生成的ID。在这里,我仅假设后端DB为Oracle/mysql
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator = "employee_sequence")
@SequenceGenerator(name = "employee_sequence", sequenceName = "EMP_SN")
private Long empNo;
这里EMP_SN
是数据库序列。
希望这能解决您的问题。
答案 2 :(得分:0)
当您使用@GeneratedValue(strategy=GenerationType.AUTO)
时,底层的ORM框架将使用标识列,序列或表,具体取决于底层的DB。在您的情况下,我猜它正在使用一个序列(用于为所有表生成ID的单个序列)。在这种情况下,为了使每个表ID都独立于其他表,必须为每个实体赋予不同的序列名称。
如果您使用的是Hibernate,请考虑使用@GenericGenerator
。来自其他SO answer的示例:
@GenericGenerator(
name = "wikiSequenceGenerator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
@Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
@Parameter(name = "initial_value", value = "1000"),
@Parameter(name = "increment_size", value = "1")
}
)
@Id
@GeneratedValue(generator = "wikiSequenceGenerator")