Java Hibernate:继承策略和持久化抽象集合时的注释
在尝试使用Hibernate持久化抽象集合时,我正在努力正确地结合继承策略和关联注释。 到目前为止,我所有的尝试都失败了,大多数都是抱怨@ManyToOne引用了一个未知的身份 给出下面的例子,我的问题是Java Hibernate:继承策略和持久化抽象集合时的注释,java,hibernate,inheritance,associations,Java,Hibernate,Inheritance,Associations,在尝试使用Hibernate持久化抽象集合时,我正在努力正确地结合继承策略和关联注释。 到目前为止,我所有的尝试都失败了,大多数都是抱怨@ManyToOne引用了一个未知的身份 给出下面的例子,我的问题是 为应用程序维护hibernate继承策略的最佳方法是什么 低于结构 哪一行id生成适合继承策略 如何注释每个类(例如@Entity、@MappedSuperclass) 如何注释方法或字段定义(例如@ManyToOne、@OneToMany) 由于我的代码嵌入到一个更大的结构中,而且每个类
- 为应用程序维护hibernate继承策略的最佳方法是什么 低于结构
- 哪一行id生成适合继承策略
- 如何注释每个类(例如@Entity、@MappedSuperclass)
- 如何注释方法或字段定义(例如@ManyToOne、@OneToMany)
package persistence;
public abstract class MechanicalObject {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public abstract void someMethod();
}
package persistence;
import java.util.List;
public abstract class Car extends MechanicalObject {
protected List<Engine> potentialEngines; // references to something that also extends MechanicalObject
protected List<Driver> potentialDrivers; // references to something that does not extend MechanicalObject
public List<Engine> getPotentialEngines() {
return potentialEngines;
}
public void setPotentialEngines(List<Engine> potentialEngines) {
this.potentialEngines = potentialEngines;
}
public List<Driver> getPotentialDrivers() {
return potentialDrivers;
}
public void setPotentialDrivers(List<Driver> potentialDrivers) {
this.potentialDrivers = potentialDrivers;
}
}
package persistence;
public abstract class Engine extends MechanicalObject {
private int driveRPM;
private Car parent; // bidirectional reference
public int getDriveRPM() {
return driveRPM;
}
public void setDriveRPM(int driveRPM) {
this.driveRPM = driveRPM;
}
public void someMethod() {
// do something
}
public abstract void anotherMethod();
}
package persistence;
public class Driver {
// db row id
private long id;
private String name;
private String licenceType;
private Car parent; // bidirectional reference
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLicenceType() {
return licenceType;
}
public void setLicenceType(String licenceType) {
this.licenceType = licenceType;
}
public Car getParent() {
return parent;
}
public void setParent(Car parent) {
this.parent = parent;
}
}
package persistence;
public class CombustionEngine extends Engine {
// db row id
private long id;
private int cylinderCapacity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getCylinderCapacity() {
return cylinderCapacity;
}
public void setCylinderCapacity(int cylinderCapacity) {
this.cylinderCapacity = cylinderCapacity;
}
public void anotherMethod() {
// do something differently
}
}
package persistence;
public class ElectroEngine extends Engine {
// db row id
private long id;
private int nominalVoltage;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getNominalVoltage() {
return nominalVoltage;
}
public void setNominalVoltage(int nominalVoltage) {
this.nominalVoltage = nominalVoltage;
}
public void anotherMethod() {
// do something
}
}
package persistence;
public class TeslaModelX extends Car {
private long id;
private String licenceNumber;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLicenceNumber() {
return licenceNumber;
}
public void setLicenceNumber(String licenceNumber) {
this.licenceNumber = licenceNumber;
}
public void someMethod() {
// do something differently
}
}
package persistence;
public class VWBeetle extends Car {
private long id;
private String serialNumber;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public void someMethod() {
// do something differently
}
}
class Car extends MechanicalObject {
@OneToMany( mappedBy="parent" )
protected List<Driver> potentialDrivers;
}
class Driver {
@ManyToOne
protected Car parent; //I'd use a better name here
}
相关的java代码如下所示:
package persistence;
public abstract class MechanicalObject {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public abstract void someMethod();
}
package persistence;
import java.util.List;
public abstract class Car extends MechanicalObject {
protected List<Engine> potentialEngines; // references to something that also extends MechanicalObject
protected List<Driver> potentialDrivers; // references to something that does not extend MechanicalObject
public List<Engine> getPotentialEngines() {
return potentialEngines;
}
public void setPotentialEngines(List<Engine> potentialEngines) {
this.potentialEngines = potentialEngines;
}
public List<Driver> getPotentialDrivers() {
return potentialDrivers;
}
public void setPotentialDrivers(List<Driver> potentialDrivers) {
this.potentialDrivers = potentialDrivers;
}
}
package persistence;
public abstract class Engine extends MechanicalObject {
private int driveRPM;
private Car parent; // bidirectional reference
public int getDriveRPM() {
return driveRPM;
}
public void setDriveRPM(int driveRPM) {
this.driveRPM = driveRPM;
}
public void someMethod() {
// do something
}
public abstract void anotherMethod();
}
package persistence;
public class Driver {
// db row id
private long id;
private String name;
private String licenceType;
private Car parent; // bidirectional reference
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLicenceType() {
return licenceType;
}
public void setLicenceType(String licenceType) {
this.licenceType = licenceType;
}
public Car getParent() {
return parent;
}
public void setParent(Car parent) {
this.parent = parent;
}
}
package persistence;
public class CombustionEngine extends Engine {
// db row id
private long id;
private int cylinderCapacity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getCylinderCapacity() {
return cylinderCapacity;
}
public void setCylinderCapacity(int cylinderCapacity) {
this.cylinderCapacity = cylinderCapacity;
}
public void anotherMethod() {
// do something differently
}
}
package persistence;
public class ElectroEngine extends Engine {
// db row id
private long id;
private int nominalVoltage;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getNominalVoltage() {
return nominalVoltage;
}
public void setNominalVoltage(int nominalVoltage) {
this.nominalVoltage = nominalVoltage;
}
public void anotherMethod() {
// do something
}
}
package persistence;
public class TeslaModelX extends Car {
private long id;
private String licenceNumber;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLicenceNumber() {
return licenceNumber;
}
public void setLicenceNumber(String licenceNumber) {
this.licenceNumber = licenceNumber;
}
public void someMethod() {
// do something differently
}
}
package persistence;
public class VWBeetle extends Car {
private long id;
private String serialNumber;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public void someMethod() {
// do something differently
}
}
class Car extends MechanicalObject {
@OneToMany( mappedBy="parent" )
protected List<Driver> potentialDrivers;
}
class Driver {
@ManyToOne
protected Car parent; //I'd use a better name here
}
包持久化;
公共抽象类MechanicalObject{
私有整数权重;
公共整数getWeight(){
返回重量;
}
公共空隙设定重量(内部重量){
重量=重量;
}
公共抽象方法();
}
包持久性;
导入java.util.List;
公共抽象类Car扩展MechanicalObject{
protected List potentialEngines;//对同时扩展MechanicalObject的内容的引用
受保护的列表潜在驱动程序;//对不扩展MechanicalObject的内容的引用
公共列表getPotentialEngines(){
返回电位发动机;
}
公共无效设置潜在引擎(列出潜在引擎){
此。潜在发动机=潜在发动机;
}
公共列表getPotentialDrivers(){
返回电位驱动程序;
}
公共无效设置潜在驱动程序(列出潜在驱动程序){
this.potentialDrivers=potentialDrivers;
}
}
包持久性;
公共抽象类引擎扩展MechanicalObject{
私人int-DRIVERRPM;
私家车家长;//双向参照
public int getDriveRPM(){
返回驱动器转速;
}
公共无效setDriveRPM(int-driveRPM){
这个.driveRPM=driveRPM;
}
公共方法(){
//做点什么
}
公开摘要无效无热法();
}
包持久性;
公务舱司机{
//数据库行id
私人长id;
私有字符串名称;
私有字符串许可类型;
私家车家长;//双向参照
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getLicenseType(){
返回许可证类型;
}
public void setLicenseType(字符串LicenseType){
this.licenseType=LicenseType;
}
公共汽车{
返回父母;
}
公共无效设置父对象(车辆父对象){
this.parent=parent;
}
}
包持久性;
公共级燃烧发动机扩展发动机{
//数据库行id
私人长id;
私人能力;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
public int getCylinderCapacity(){
回油缸容量;
}
公共无效设置气缸容量(内部气缸容量){
此。气缸容量=气缸容量;
}
公共无效无热法(){
//做些不同的事情
}
}
包持久性;
公共类电子引擎扩展引擎{
//数据库行id
私人长id;
专用标称电压;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
public int getNominalVoltage(){
返回标称电压;
}
公共无效设置标称电压(int标称电压){
此参数。标称电压=标称电压;
}
公共无效无热法(){
//做点什么
}
}
包持久性;
公共级TeslaModelX扩展汽车{
私人长id;
专用字符串许可证号码;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getLicenseNumber(){
返回许可证号码;
}
public void setLicenseNumber(字符串LicenseNumber){
this.licenseNumber=LicenseNumber;
}
公共方法(){
//做些不同的事情
}
}
包持久性;
大众级甲壳虫车{
私人长id;
私有字符串序列号;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getSerialNumber(){
返回序列号;
}
公共无效设置序列号(字符串序列号){
this.serialNumber=serialNumber;
}
公共方法(){
//做些不同的事情
}
}
如果有任何帮助,我将不胜感激。我也欢迎链接到解释解决方案的网页,因为我找不到任何能完全解决这个问题的网页
为下面的结构维护hibernate继承策略的最佳方法是什么
这取决于模型的其余部分是什么样子。最容易使用的策略是单表策略,但如果子类差异太大,则使用联接表策略或按类表策略可能会更好。只需仔细阅读这些策略,然后选择一种您熟悉的策略。但是,请注意,很难进一步更改继承策略层次结构-如果可能的话(可能取决于JPA提供商)