首页 \ 问答 \ 为什么我要用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

相关问答

更多

Hibernate / JPA和PostgreSQL - 主键?(Hibernate/JPA and PostgreSQL - Primary Key?)

GenerationType.IDENTITY I've solved the issue. Previously -- in my MySQL implementation -- I made use of an abstract base class with the following signature: @MappedSuperclass public abstract class AbstractDomainEntity implements Serializable { @ ...

从共享指针的解除引用值获取共享指针(Get a shared pointer from a dereferenced value of a shared pointer)

通常,您不能只“找到”指向对象的指针,因为如果指向特定对象,则无法跟踪。 您应该自己设计这样的机制,或者使用标准库提供的机制: enable_shared_from_this 。 第1步:从std::enable_shared_from_this派生你的类,并提供一个成员函数来获取指针: #include <memory> struct ClassA: std::enable_shared_from_this<ClassA> { std::shared_ptr<ClassA> get_p ...

JPA Hibernate帮助!(JPA Hibernate Help!)

代替 Query query = entityManager.createNativeQuery("SELECT * FROM person"); List<Model> models = query.getResultList(); 你可以试试 - List<Model> models = entityManager.createQuery("from Model",Model.class).getResultList(); 我没有看到在这里使用本机查询的任何目的。 Instead of ...

JPA MappedSuperClass(JPA MappedSuperClass)

您不应该在子类中再次定义字段:请查看: https : //stackoverflow.com/a/5258090/286588 You should not define the fields again in child class : check this out : https://stackoverflow.com/a/5258090/286588

Hibernate JPA QueryBuilder.like(Hibernate JPA QueryBuilder.like)

将搜索包装到'%'+'%' - > builder.like(root.get(Book_.ispn),'%'+ search +'%')。 更好地创建util方法并重用它。 喜欢 : public staic String wrapToLike(String value) { return value == null ? "%" : "%"+value"%"; } 要么 : //root.get(Book_.name) - path public Predic ...

与原始类型联合反对自我的JPA标准(JPA Criteria with Join against self with primitive types)

您只能通过1-1 / 1-N / MN / N-1关系加入,因为JPA试图保持OO的精神。 你的替代品是 添加一个额外的from子句并指定一个where子句。 这可能会创建一个CROSS JOIN。 为模型添加关系,然后您可以使用join ()并定义INNER或LEFT OUTER。 显然,其中一些可能不适合您的情况。 You can only join via a 1-1/1-N/M-N/N-1 relation, since JPA tries to keep to the spirit of ...

JPA查询获取整棵树(JPA query for getting the whole tree)

简短的答案是; 没有没有一个标准的方式来做到这一点。 你必须使用原生的SQL。 您可能能够扩展Oracle Hibernate Dialect并添加一些用户函数/扩展来获得hibernate来生成PRIOR或CONNECT BY子句,但这会阻止您的应用程序严格遵守JPA和数据库独立。 The short answer is; no there isn't a standard way to do this. You have to use native sql. You may be able t ...

JPA NamedQuery参数(JPA NamedQuery parameters)

您不能使用Character.isJavaIdentifierPart定义的不属于有效标识符的字符 。 命名参数在JPA 2.0规范中定义: Java持久性查询语言查询的命名参数是以“:”符号为前缀的标识符。 传递给Query和TypedQuery接口的setParameter方法的参数名称不包含此“:”前缀。 ... 标识符是无限长度的字符序列。 字符序列必须以Java标识符起始字符开头,所有其他字符必须是Java标识符部分字符。 标识符开始字符是Character.isJavaIdentifi ...

相关文章

更多

最新问答

更多
  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)