简体   繁体   English

Java类存储在数据库中的什么位置?

[英]Where are java classes stored in database?

I need to suggest three variants of the structure of the database that reflects object model of classes. 我需要建议数据库结构的三种变体,以反映类的对象模型。 The structure of the database should allow understand class inheritance. 数据库的结构应允许了解类继承。 For simplicity, classes contains only attributes (no methods). 为简单起见,类仅包含属性(不包含方法)。

I consider Single Table Inheritance, Class Table Inheritance, Concrete Table Inheritance patterns. 我考虑了单表继承,类表继承,具体表继承模式。

But not everything is clear for me. 但是并不是所有事情对我来说都是清楚的。 The question are: 问题是:

1) Where are classes stored in database, and where are instances of classes stored? 1)类存储在数据库中的什么地方,类的实例存储在哪里?

2) Let's say, class B extends class A . 2)假设class B class A扩展了class A How to make instance b of the B understand that it is a derived A ? 如何使B实例b了解它是派生的A How to make b understand that this is an instance of class B , not class A ? 如何使b理解这是class B class A的实例,而不是class A class B的实例?

3) Is there a patter in which an addition of a new class to inheritance hierarchy does not require changing the structure of tables? 3)是否有一种模式,其中在继承层次结构中添加新类不需要更改表的结构?

4) Is there a pattern which allows multiple inheritance? 4)是否有允许多重继承的模式?

UPD: Probably I expressed my thoughts not clear enough. UPD:可能我表达的想法不够清楚。 Let's say it is not java, but any another language. 假设它不是Java,而是任何其他语言。 This quiesion is in context of database structure. 该问题与数据库结构有关。 Is there pattern (like above) which allows multiple inheritance? 是否存在允许多重继承的模式(如上)?

Thanks. 谢谢。

1) Where are classes stored in database, and where are instances of classes stored? 1)类存储在数据库中的什么地方,类的实例存储在哪里?

Classes are not stored in the database, only instances. 类不存储在数据库中,仅存储在实例中。 In most cases/mappers a discriminator tells the mapper the type/class of an instance. 在大多数情况下,鉴别器会告诉映射器实例的类型/类。

2) Let's say, class B extends class A. How to make instance b of the B understand that it is a derived A? 2)假设类B扩展了类A。如何使B的实例b了解它是派生的A? How to make b understand that this is an instance of class B, not class A? 如何使b理解这是B类而不是A类的实例?

Again this is normally done via discriminator columns. 同样,这通常是通过区分符列完成的。

As for "How to make instance b of the B understand that it is a derived A": you do that in the code, ie B extends A . 至于“如何使B的实例b理解它是派生的A”:您可以在代码中做到这一点,即B extends A If you change that later you might break existing data so you'd either need to think about inheritance when designing the model or have some migration strategy when you need to make changes. 如果以后进行更改,则可能会破坏现有数据,因此在设计模型时需要考虑继承,或者在进行更改时需要一些迁移策略。

3) Is there a patter in which an addition of a new class to inheritance hierarchy does not require changing the structure of tables? 3)是否有一种模式,其中在继承层次结构中添加新类不需要更改表的结构?

This could be achieved with table-per-class or joined strategy. 这可以通过按班级分配表或联合策略来实现。 In both cases you'd need to add tables for new subclasses but you'd not have to alter the existing ones (provided you already have a discriminator column, otherwise you'd have to add that once). 在这两种情况下,都需要为新的子类添加表,但不必更改现有的子类(前提是您已经有一个discriminator列,否则必须添加一次)。

4) Is there a patter which allows multiple inheritance? 4)是否有允许多重继承的模式?

No, Java doesn't allow multiple inheritance (this applies to orm as well) and it's not needed in most cases anyways. 不,Java不允许多重继承(这也适用于orm),而且在大多数情况下都不需要。 Try composition instead. 尝试合成。

1) I don't know 1)我不知道

2) In a if statement you can ask if it is and instance of an object like this: 2)在if语句中,您可以询问它是否是和这样的对象实例:

if ( obj instanceof A ) {
    obj.method1();
} else {
    obj.method2();
}

3) That is possible when the class is not 'abstract'. 3)当班级不是“抽象”时,这是可能的。 And when there are no abstract methods. 并且当没有抽象方法时。

4) You can not use more inheritance then one. 4)您不能再使用一个继承。 But you can implement more interfaces the one. 但是您可以实现更多的接口之一。 Maybe this is the way for you. 也许这就是您的方式。 Otherwise you have to use a inheritance of more classes. 否则,您必须使用更多类的继承。 So you get a sub-, sub-, sub- class or something. 这样就得到了子类,子类,子类之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM