0
我有這樣的情況: 有幾組POJO類,每個都有一個父類。每個小組都負責處理一些數據記錄 - 在JAXB的幫助下將XML轉換爲XML,並在Hibernate的幫助下轉換成數據庫。我的工作代碼的簡化版本是:爲什麼hibernate不會在表中寫入任何信息?
public static abstract class Habit{
String habitName;
/* constructors */
/* getter & setter */
}
@Entity
@Access(AccessType.PROPERTY)
@Table(name = "habit")
public static class DBHabit extends Habit{
@Id
@GeneratedValue
private int id;
@Column(name = "habit_name")
public String getHabitName() {
return habitName;
}
public void setHabitName(String habitName) {
this.habitName = habitName;
}
}
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "habit")
public static class XmlHabit extends Habit{
@XmlElement
public String getHabitName() {
return habitName;
}
public void setHabitName(String habitName) {
this.habitName = habitName;
}
}
public static abstract class Person{
int age;
String name;
List<Habit> habits;
/* Constructor */
/* Getters & Setters */
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Habit> getHabits() {
return habits;
}
public void setHabits(List<Habit> habits) {
this.habits = habits;
}
}
@Entity
@Access(AccessType.PROPERTY)
@Table(name = "person")
public static class DBPerson extends Person{
@Id
@GeneratedValue
private int id;
@Column(name = "age")
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Column(name = "name")
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = DBHabit.class)
@Override
public List<Habit> getHabits() {
return habits;
}
@Override
public void setHabits(List<Habit> friends) {
this.habits = habits;
}
}
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "", propOrder = {
"age",
"name",
"habits"
})
@XmlRootElement(name = "state")
public static class XmlPerson extends Person{
@Override
@XmlElement(required = true)
public int getAge() {
return super.getAge();
}
@Override
public void setAge(int age) {
super.setAge(age);
}
@Override
@XmlElement(required = true)
public String getName() {
return super.getName();
}
@Override
public void setName(String name) {
super.setName(name);
}
@Override
@XmlElement(required = true)
@XmlElementWrapper(name = "friends")
public List<Habit> getHabits() {
return super.getHabits();
}
@Override
public void setHabits(List<Habit> habits) {
super.setHabits(habits);
}
}
我在使用Hibernate時遇到問題。以爲我寫到我期望在targetEntity中記錄DBHabit.class,hibernate不記錄任何關於習慣的信息,這個表總是空的。 請給我一個提示,我可以在這種情況或建議下做些什麼,如何以不同的方式爲這樣的寫作製作一個類似的抽象系統。 感謝您的關注!