您好我有一个类车辆是父类,两个Wheeler和Four Wheeler是子类,它扩展了父类。
问题是当我将数据插入数据库中的 rto(id,name,vehicle_type,steeringHandle,steeringWheel)表时,它正在为 TwoWheeler 插入已分配的数据但是对于 FourWheeler ,它插入了父类中存在的方法的数据,但是当我从子类(FourWheeler(setsteeringWheel))调用方法时,它没有更新指定的中的数据
steeringWheel专栏。我正在添加表格的屏幕截图
车辆类
`
package hibernate_discriminator;
import java.io.Serializable;
public class Vehicle implements Serializable {
private int id;
private String name;
public Vehicle(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id= id;
}
public void setName(String name){
this.name= name;
}
public String getName(){
return name;
}
}
TwoWheeler Class
`package hibernate_discriminator;
public class TwoWheeler extends Vehicle{
private String steeringHandle;
public TwoWheeler(){
}
public void setsteeringHandle(String handle){
this.steeringHandle= handle;
}
public String getsteeringHandle(){
return steeringHandle;
}
}
`
FourWheeler Class
package hibernate_discriminator;
public class FourWheeler extends Vehicle {
private String steeringWheel;
public FourWheeler(){
}
public void setsteeringWheel(String wheel){
this.steeringWheel= steeringWheel;
}
public String getsteeringWheel(){
return steeringWheel;
}
}
映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate_discriminator.Vehicle" table="rto" discriminator-value="vehicle">
<id name="id" column="id" type="integer">
<generator class="increment"></generator>
</id>
<discriminator column="vehicle_type" type="string"></discriminator>
<property name="name" column="name" type="string"></property>
<subclass name="hibernate_discriminator.TwoWheeler" extends="Vehicle" discriminator-value="Bike">
<property name="steeringHandle" column="steeringHandle" type="string"></property>
</subclass>
<subclass name="hibernate_discriminator.FourWheeler" extends="Vehicle" discriminator-value="Car">
<property name="steeringWheel" column="steeringWheel" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">ankita</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="resources/Vehicle.hbm.xml"/>
</session-factory>
</hibernate-configuration>
主类
public class Main {
public static void main(String[] args){
Configuration cfg= new Configuration();
cfg.configure("/resources/Config.cfg.xml");
SessionFactory sf= cfg.buildSessionFactory();
Session s= sf.openSession();
Transaction tx= s.beginTransaction();
Vehicle v= new Vehicle();
v.setName("Vehicle");
TwoWheeler tw= new TwoWheeler();
tw.setName("CBR");
tw.setsteeringHandle("CBR Handle");
FourWheeler fw= new FourWheeler();
fw.setName("Audi A4");
fw.setsteeringWheel("Audi a7 Wheel");
s.save(v);
s.save(tw);
s.save(fw);
s.flush();
tx.commit();
}
}
FourWheeler Class
public class Main {
public static void main(String[] args){
Configuration cfg= new Configuration();
cfg.configure("/resources/Config.cfg.xml");
SessionFactory sf= cfg.buildSessionFactory();
Session s= sf.openSession();
Transaction tx= s.beginTransaction();
Vehicle v= new Vehicle();
v.setName("Vehicle");
TwoWheeler tw= new TwoWheeler();
tw.setName("CBR");
tw.setsteeringHandle("CBR Handle");
FourWheeler fw= new FourWheeler();
fw.setName("Audi A4");
fw.setsteeringWheel("Audi a7 Wheel");
s.save(v);
s.save(tw);
s.save(fw);
s.flush();
tx.commit();
}
}
答案 0 :(得分:4)
我认为你的问题就在这里......
public void setsteeringWheel(String wheel){
this.steeringWheel= steeringWheel;
}
方法参数为wheel
,但您指定的值为steeringWheel
。 (您正在分配this.steeringWheel = steeringWheel
,这意味着在调用null
后,它将具有相同的值setsteeringWheel()
。)
答案 1 :(得分:1)
问题在这里
public void setsteeringWheel(String wheel){
this.steeringWheel= steeringWheel;
}
您没有使用传递的参数 this.steeringWheel = wheel;