我将json文件上传到H2DB。我在外键中得到的是空引用。
学生实体:
@Entity
public class Student {
@Id
@Column(name="STUDENT_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + "]";
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
//@Embedded
@OneToOne(cascade=CascadeType.ALL)
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址实体:
//@Embeddable
@Entity
public class Address {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
int aid;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
String street,city;
int zip;
public Address() {
// TODO Auto-generated constructor stub
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
我需要地址表中的学生ID作为外键。但是我在学生表中有地址ID
这是我要上传的JSON文件 JSON:
[
{
"name" : "Lakshmi",
"address" :
{
"street": "kattabomman",
"city" : "Coimbatore",
"zip" : "641035"
}
},
{
"name" : "Pravin",
"address" :
{
"street": "kattabomman",
"city" : "Coimbatore",
"zip" : "641035"
}
},
{
"name" : "Ranjith",
"address" :
{
"street": "k",
"city" : "Coe",
"zip" : "641"
}
} ]
我在哪里犯了错误? 预先感谢。