Table Per Class继承:获取空值

时间:2017-01-15 19:11:11

标签: java mysql hibernate table-per-class

我编写了Table Per Class继承实现的代码,该代码显示在youtube tutorial中。在我的代码中,我有StoreData.java,它将对象和Vehicle.java保存为父类,而TwoWheeler.java,FourWheeler.java作为子类。

StoreData.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class StoreData {
    public static void main(String[] args) {

        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction t = session.beginTransaction();

        Vehicle vehicle = new Vehicle();
        vehicle.setVehicle_Name("Car");

        TwoWheeler twoWheeler = new TwoWheeler();
        twoWheeler.setBikeModel("HONDA CBR 250");

        FourWheeler fourWheeler = new FourWheeler();
        fourWheeler.setFourWheelerModel("HONDA City");

        session.save(vehicle);
        session.save(twoWheeler);
        session.save(fourWheeler);

        t.commit();
        session.close();


    }
}

Vehicle.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Vehicle {

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private int vehicle_id;
    private String Vehicle_Name;


    public int getVehicle_id() {
        return vehicle_id;
    }
    public void setVehicle_id(int vehicle_id) {
        this.vehicle_id = vehicle_id;
    }
    public String getVehicle_Name() {
        return Vehicle_Name;
    }
    public void setVehicle_Name(String vehicle_Name) {
        Vehicle_Name = vehicle_Name;
    }



}

TwoWheeler.java

import javax.persistence.Entity;

@Entity
public class TwoWheeler extends Vehicle {
    private String bikeModel;

    public String getBikeModel() {
        return bikeModel;
    }

    public void setBikeModel(String bikeModel) {
        this.bikeModel = bikeModel;
    }

}

FourWheeler.java

import javax.persistence.Entity;

@Entity
public class FourWheeler extends Vehicle {
    private String fourWheelerModel;

    public String getFourWheelerModel() {
        return fourWheelerModel;
    }

    public void setFourWheelerModel(String fourWheelerModel) {
        this.fourWheelerModel = fourWheelerModel;
    }

}

我使用了MySql,表格中的数据如下所示。 表车:

vehicle_d  vehicle_name
   1           Car

TwoWheeler表:

vehicle_id  vehicle_name  bikemodel
 2            null        HONDA CBR 250

FourWheeler表:

vehicle_id  vehicle_name  bikemodel
3            null          HONDA City

这里在vehicle_name列中我得到的是null而不是Car。我做错了什么请告诉。

1 个答案:

答案 0 :(得分:0)

您应该使用小写而不是大写,所以:

更改

的第一个字母
private String Vehicle_Name;

致:

private String vehicle_Name;

您可以详细了解How do getters and setters work?

希望这可以帮到你。