HTTP状态500 - 保存休眠OneTOMany关联时的内部服务器错误

时间:2015-12-09 09:01:38

标签: java hibernate rest

我正在执行OneToMany关系,数据正确保存,但apache tomcat服务器提供错误HTTP状态500 - 内部服务器错误。

捐助者类别:

@Entity
public class DonorClass {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int donorId;
private String firstName;
private String lastName;
private int age;
private String cnic;
private String contactNumber;
private String homeNumber;
private String country;
private String city;
private String town;
private String streetNo;
private String houseNo;
private String email;

@OneToMany(cascade = CascadeType.ALL, mappedBy="donor")
private Set<BloodDonorClass> blood;

public int getDonorId() {
    return donorId;
}
public void setDonorId(int donorId) {
    this.donorId = donorId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getCnic() {
    return cnic;
}
public void setCnic(String cnic) {
    this.cnic = cnic;
}
public String getContactNumber() {
    return contactNumber;
}
public void setContactNumber(String contactNumber) {
    this.contactNumber = contactNumber;
}
public String getHomeNumber() {
    return homeNumber;
}
public void setHomeNumber(String homeNumber) {
    this.homeNumber = homeNumber;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getTown() {
    return town;
}
public void setTown(String town) {
    this.town = town;
}
public String getStreetNo() {
    return streetNo;
}
public void setStreetNo(String streetNo) {
    this.streetNo = streetNo;
}
public String getHouseNo() {
    return houseNo;
}
public void setHouseNo(String houseNo) {
    this.houseNo = houseNo;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

public Set<BloodDonorClass> getBlood() {
    return blood;
}

public void setBlood(Set<BloodDonorClass> blood) {
    this.blood = blood;
}|
}

BloodDonorClass类:

@Entity
public class BloodDonorClass {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int bloodId;
private String bloodType;
private int price;

@ManyToOne
@JoinColumn(name="donor_id")
private DonorClass donor;

public int getBloodId() {
    return bloodId;
}
public void setBloodId(int bloodId) {
    this.bloodId = bloodId;
}
public String getBloodType() {
    return bloodType;
}
public void setBloodType(String bloodType) {
    this.bloodType = bloodType;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}

public DonorClass getDonor() {
    return donor;
}

public void setDonor(DonorClass donor) {
    this.donor = donor;
}
}

Blood And Donor Object类:

public class BloodDonorObjectClass {

private DonorClass donor;
private BloodDonorClass blood;
public DonorClass getDonor() {
    return donor;
}
public void setDonor(DonorClass donor) {
    this.donor = donor;
}
public BloodDonorClass getBlood() {
    return blood;
}
public void setBlood(BloodDonorClass blood) {
    this.blood = blood;
}
}

BloodDonorClassService类:

SessionFactory sessionFactory = null;

public BloodDonorObjectClass addNewDonor(BloodDonorObjectClass bloodDonor){
    try{
        DonorClass donor = new DonorClass();
        BloodDonorClass blood = new BloodDonorClass();
        blood = (BloodDonorClass)bloodDonor.getBlood();
        donor = (DonorClass)bloodDonor.getDonor();
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        blood.setDonor(donor);
        HashSet<BloodDonorClass> bloods = new HashSet<BloodDonorClass>();
        bloods.add(blood);
        donor.setBlood(bloods);
        session.save(donor);
        session.getTransaction().commit();
        session.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return bloodDonor;
}
}

血液资源类:

@Path("bloodresource")
public class BloodResourceNew {

BloodDonorClassService bloodService = new BloodDonorClassService();

@Path("new")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public BloodDonorObjectClass addNew(BloodDonorObjectClass blood){
    return bloodService.addNewDonor(blood);
}
}

控制台输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into DonorClass (age, city, cnic, contactNumber, country, email, firstName, homeNumber, houseNo, lastName, streetNo, town, donorId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into BloodDonorClass (bloodType, donor_id, price, bloodId) values (?, ?, ?, ?)

我将感恩:)

0 个答案:

没有答案