首页 \ 问答 \ 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)

为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)

我有一个关于JPA和继承(EclipseLink)的问题:

对于一个学校项目,我创建了一个抽象类:HumanEntity和实现它的不同子类。

抽象类:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class HumanEntity implements IHuman{

@Id
@GeneratedValue
protected long      id;

@Column(name = "FIRST_NAME")
protected String    firstName;

@Column(name = "LAST_NAME")
protected String    lastName;

protected LocalDate birthDate;

protected HumanEntity(){

}

@Override
public String getFirstName() {
    return firstName;
}

@Override
public String getLastName() {
    return lastName;
}

@Override
public LocalDate getBirthDate() {
    return birthDate;
}

}

子类示例:

@Entity
@Table(name="ACTOR")
public class ActorEntity extends HumanEntity{

protected ActorEntity(){}

protected ActorEntity(ActorBuilder actorBuilder){
    this.firstName  = actorBuilder.getFirstName();
    this.lastName = actorBuilder.getLastName();
    this.birthDate  = actorBuilder.getBirthDate();
}

}

当我运行项目时: - >我为每个子类共享ID

在演员表中:1 - > .... 3 - > ....

在导演表中:2 - > ....

如何为每个子类创建不同的Id,但在我的代码中使用受保护(共享)ID?

另一个小问题EclipseLink或JPA总是创建一个序列表,但我不知道它用于什么? 我可以删除吗?

非常感谢 !


I've a question about JPA and Inheritance (EclipseLink) :

For a school project, I've created an abstract class : HumanEntity and different subclasses which implement it.

The abstract class:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class HumanEntity implements IHuman{

@Id
@GeneratedValue
protected long      id;

@Column(name = "FIRST_NAME")
protected String    firstName;

@Column(name = "LAST_NAME")
protected String    lastName;

protected LocalDate birthDate;

protected HumanEntity(){

}

@Override
public String getFirstName() {
    return firstName;
}

@Override
public String getLastName() {
    return lastName;
}

@Override
public LocalDate getBirthDate() {
    return birthDate;
}

}

A subclass example :

@Entity
@Table(name="ACTOR")
public class ActorEntity extends HumanEntity{

protected ActorEntity(){}

protected ActorEntity(ActorBuilder actorBuilder){
    this.firstName  = actorBuilder.getFirstName();
    this.lastName = actorBuilder.getLastName();
    this.birthDate  = actorBuilder.getBirthDate();
}

}

When I run the the project : -> Ids are shared for every subclasses then I have

In the Actor Table : 1 -> .... 3 -> ....

In the director table : 2 -> ....

How can I create a different Id for every subclasses but with a protected (shared) Id in my code ?

Another little question EclipseLink ,or JPA always create a sequence table but i dont know what is it used for ? can I delete it ?

Thanks a lot !


原文:https://stackoverflow.com/questions/35927484
更新时间:2019-07-13 09:05

最满意答案

HumanEntity类型或子类的所有对象都是HumanEntity的实例,因此id需要保持一致,因此需要来自相同的“组”ID。

对于Actor 你不能说id为1,因为它们都是HumanEntity ,因此你不能说ID为1,你需要唯一地标识一个HumanEntity 。 即Actor#1也是HumanEntity#1。 因此,你也不能拥有导演#1,因为那也是HumanEntity#1!

如果你想拥有像那样的id那么你需要改变你的继承结构。


All objects that are of type HumanEntity or subclasses are instances of HumanEntity, hence the id's need to be consistent, and hence from the same "group" of ids.

You CANNOT have say id 1 for an Actor and id 1 for a Director since they are both HumanEntity, and you need to uniquely identify a HumanEntity. i.e Actor#1 is also HumanEntity#1. So you cannot also have Director#1 since that would also be HumanEntity#1!

If you want to have id's like those then you would need to change your inheritance structure.

2016-03-11

相关问答

更多

需要在htaccess中重写包含#tag的URL(Need to rewrite URL contain # tag in htaccess)

简短的回答是:使用.htaccess根本不可能! 一个更长的答案: #字符后面的部分不会从浏览器发送到Web服务器,因为它仅用于客户端。 必须使用客户端JavaScript进行这种“重写”。 hashchange事件可能是您的起点: window.addEventListener('hashchange', function() { var hash = location.hash.substr(1); window.location.href = "/" + hash + ".

在Tensorflow中创建高度可自定义的RNN(Creating a highly customizable RNN in Tensorflow)

import tensorflow as tf import numpy as np hidden_size = 2 # hidden layer of two neurons input_size = 5 # Weight of x will the be (hidden_layer_size x input_size) Wx = tf.Variable(tf.random_normal([hidden_size, input_size], stddev=0.35),

为什么Bash with Double Brackets(“[[”)中的等式运算符(“==”)的顺序很重要?(Why does Order Matter for the Equality Operator (“==”) in Bash with Double Brackets (“[[”)?)

正确的参数是专门处理的,它可以是通配符模式,但左参数不能。 从手册 : 当使用'=='和'!='运算符时,运算符右侧的字符串被视为模式,并根据模式匹配中描述的规则进行匹配,就像启用了extglob shell选项一样。 '='运算符与'=='相同。 如果启用了nocasematch shell选项(请参阅Shopt Builtin中的shopt说明),则执行匹配而不考虑字母字符的情况。 如果字符串匹配('==')或与模式不匹配('!='),则返回值为0,否则返回1。 可引用图案的任何部分以迫使引

创建一个从阻塞线程订阅事件的表单(Creating a form that subscribes to events from a blocking thread)

您正在创建两个相同的表单: worker.DebugForm = new PBForm2(worker.Id); var debugForm = new PBForm2(worker.Id); 然后加载debugForm,但是您的更新正在对DebugForm.picturebox1进行更新,因此您的更新将不会被看到。 需要对debugForm.picturebox1进行更新,但是您应该只创建一个。 在没有看到所有代码的情况下,为什么不在工作类中加载一个代码或者将一个代码指向另一个代码呢? Ap

在sqlalchemy中为s选择添加一个空列(adding a null column to s selection in sqlalchemy)

您可以使用sqlalchemy.sql.expression.null from sqlalchemy.sql.expression import null db.session.query(models.User.id, null()) You can use sqlalchemy.sql.expression.null from sqlalchemy.sql.expression import null db.session.query(models.User.id, null())

订阅值在component.ts中返回undefined但在service.ts中存在值(subscribed value returns undefined in component.ts but value exists in service.ts)

您应该在map返回结果 getImages(sheetName) { const apiServerEndPoint = '/api' + sheetName; return this.http.get(apiServerEndPoint) .map((res: Response) => { console.log(res.json()); /* You need to return the

代码不适用于无限列表(Code doesnt work with infinite lists)

您的实现问题是使用length 。 使用带有无限列表的length作为参数不是一个好主意,因为,列表无限长。 我建议改变实现以使用可以在无限列表上工作的其他函数,例如drop和take : function :: Int -> Int -> [a] -> [a] function i j list = take (i - j + 1) (drop (i - 1) list) 或者,无点: function i j = take (j - i + 1) . drop (i - 1) The p

从String中提取数字(Extract number from a String)

正则表达式可以像这样使用: public static void main(String args[]) { String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK POSTCODE 60025"; System.out.println(text1.replaceAll(".*?\\b(6000\\d+)\\b.*", "$1")); // replace everything except a number

相关文章

更多

最新问答

更多
  • 图像TapGestureRecognizer未触发(Image TapGestureRecognizer not firing)
  • 从数组中选择项目(Selecting items from an array)
  • PhpStorm:简单的PHP表单不发布(PhpStorm: Simple PHP form not posting)
  • 如果语句只有Javascript中的数字(If statement with only a number in Javascript)
  • 截图Android中的黑色(Screenshot Black in Android)
  • 默认值如何在数据库中内部工作?(How does default value internally work in Database?)
  • 左连接不加入单个记录(Left Join Not Joining with a Single Record)
  • 在#sign drupal之后获取url参数(get url parameter after # sign drupal)
  • Crontab CD到目录(Crontab CD to Directory)
  • #inf c ++ visual studio(#inf c++ visual studio)
  • 使用Python将指数修改的高斯曲线拟合到数据(Fitting an exponential modified gaussian curve to data with Python)
  • Javascript - 具有音高和持续时间控制的文本到语音(Javascript - text to speech with pitch and duration control)
  • 群组和用户有多少通过?(Groups and Users has many oder has many through?)
  • 如何在简单的二叉树中选择节点所在的哪一侧?(How to pick which side a node is on in a simple binary tree?)
  • 在ImageView上添加TextView(Android)(Add TextView over ImageView (Android))
  • 使用工厂方法创建泛型(Using Factory Method to Create Generics)
  • cordova 4.1.2中的平台特定代码(platform specific code in cordova 4.1.2)
  • 使用php格式化日期[复制](Format date using php [duplicate])
  • 在Python中解析年,月,日,小时,分钟,秒(Parsing year, month, day, hour, minute, second in Python)
  • 用jquery替换图像和类(Replacing image and class with jquery)
  • 导入的module.submodule命名空间干扰彼此(imported module.submodule namespaces interfering with eachother)
  • 播放框架:提交按钮似乎不起作用(Play framework: Submit button doesn't seem to work)
  • 段落包括通过过渡带来的div(Paragraph covers div brought through transition)
  • 使用processing.Manager时对象是否重复?(Object duplicated when using processing.Manager?)
  • lxde意外退出(lxde quits unexpectedly)
  • 真正的Maven依赖是什么?(What really are Maven dependencies?)
  • 如何在我的代码中修复getJSON方法错误?(how to fix getJSON method error in my code?)
  • Google Analytics状态:已安装跟踪 - 但无法正常运行(Google Analytics Status: Tracking Installed - but isnt working)
  • 在模型文件中的非对象上调用成员函数num_rows()(Call to a member function num_rows() on a non-object in model file)
  • 有关UNIX中fork()函数的问题(Questions about the fork() function in UNIX)