- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下方案:TableA1和TableA2存在于数据库中,每个都由一个实体bean表示。由于它们是相关的,我创建了一个抽象类(TableA,它是一个实体,但在数据库中不存在),其中两个实体都继承自该类。另外,TableA与TableB具有一对一的关系。
我的目标是查询 TableB,并根据类型从那里获取 TableA1 或 TableA2 的信息。
TableA1和TableA2各有一个id(每个表自动生成一个序号,因此可能会出现重复)。
在 TableB 中,我有两列组合起来代表外键:type 和 id。 Type = 1 表示 id 在 TableA1 中。与 TableA2 类似。
我的问题是我不知道如何将这两列定义为外部外键。这就是我所得到的:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="type")
public abstract class TableA {
@Id
@Column(name = "type")
protected int type;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
protected int id;
@Column(name = "name")
private String name;
// Getters and setters
}
@Entity
@DiscriminatorValue("1")
@Table (name="tableA1")
public class TableA1 extends TableA {
@Column(name="col1")
private String col1;
// Getters and setters
}
@Entity
@DiscriminatorValue("2")
@Table (name="tableA2")
public class TableA2 extends TableA {
@Column(name="col2")
private String col2;
// Getters and setters
}
@Entity
@Table (name="tableB")
public class TableB {
@Id
@Column(name="someId")
private Integer someId;
@Column(name="type")
private int type;
@Column(name="id")
private Integer id;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "type"),
@JoinColumn(name = "id" )
})
private TableA tableA;
// Getters and setters
}
更新
我在寻找不可能的事情吗?这是我发现的:
Polymorphic relations to non-leaf classes in a table-per-class hierarchy have many limitations. When the concrete subclass is not known, the related object could be in any of the subclass tables, making joins through the relation impossible. This ambiguity also affects identity lookups and queries; these operations require multiple SQL SELECTs (one for each possible subclass), or a complex UNION.
更新2
数据库中已存在表A1、表A2和表B,并且具有以下结构:
CREATE TABLE TableA1 (
surrogate_key int AUTO_INCREMENT,
some_char char(30),
PRIMARY KEY (surrogate_key)
);
CREATE TABLE TableA2 (
surrogate_key int AUTO_INCREMENT,
some_int int,
PRIMARY KEY (surrogate_key)
);
CREATE TABLE TableB (
surrogate_key int AUTO_INCREMENT,
type int, // if type=1, sk represents the surrogate_key of tableA1
// if type=2, sk represents the surrogate_key of tableA2
sk int,
description varchar(200),
PRIMARY KEY (surrogate_key)
);
最佳答案
更新答案:
已更新以匹配数据库
您可以使用 getDiscriminatorValue() 来访问 DiscriminatorValue。
像这样定义映射:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public abstract class TableA implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "surrogate_key")
protected int id;
@Id
@Column(name = "type")
protected int type;
// Constructors & getters/setters
@Transient
public String getDiscriminatorValue() {
DiscriminatorValue val = this.getClass().getAnnotation(DiscriminatorValue.class);
return val == null ? null : val.value();
}
}
@Entity
@DiscriminatorValue("1")
public class TableA1 extends TableA {
@Column(name = "some_char", length = 1)
private char someChar;
// Constructors & getters/setters & toString/equals
}
@Entity
@DiscriminatorValue("2")
public class TableA2 extends TableA {
@Column(name = "some_int")
private int someInt;
// Constructors & getters/setters & toString/equals
}
@Entity
public class TableB implements Serializable {
@Id
@GeneratedValue
@Column(name = "surrogate_key")
private int id;
@OneToOne
@Cascade(value = CascadeType.SAVE_UPDATE)
@JoinColumns({@JoinColumn(name = "sk", referencedColumnName = "surrogate_key"),
@JoinColumn(name = "type", referencedColumnName = "type")})
private TableA tableA;
@Column(name = "description", length = 200)
private String description;
// Constructors & getters/setters & toString/equals
}
并像这样查询:
newSession.createQuery("from TableB tb where tb.tableA.type=:type order by tb.id asc").setParameter("type", 1));
关于java - JPA实体: inheritance and one-to-one relationship not working simultaneously,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624410/
此函数返回一个 2 元组,其中包含列表的最小值和最大值: import Control.Arrow ((***), (>>>), (&&&)) import Data.Semigroup (getMi
我有两个 Java 进程,Processor 和 Simulator。 模拟器每5秒向File X写入规定的数据记录(10行文本)。 处理器不断读取该文件,等待 token 指示有效记录。代码是这样的
我有两个不同的应用程序必须协同工作。进程 1 作为时间源,进程 2 根据进程 1 提供的时间源执行操作。我需要运行进程 2 的多个副本。目标是让一个时间源进程同时向 5-10 个其他进程发出信号时间,
在我的 Backbone.js 应用程序中,我有以下模型和 View : var Operation = Backbone.Model.extend({ defaults: function(
请大家帮忙,这很重要。 我需要在不到 5 分钟的时间内下载大约 2000 张图像。所以我决定进行并行图片下载。 如果有人可以建议我进行并行下载的更好方法或告诉我哪里出错了,我将不胜感激。 在错误日志中
当我阅读 Clojure 代码(比如来自 GitHub 上的各个项目)时,有一些东西我不太理解:在 :require 表达式中,你应该使用 :as 当您仅使用您使用 :refer 引用的函数时?另外,
我在多个客户端连接的客户端服务器聊天程序的客户端实现上遇到问题。问题是我遇到的问题是我应该如何同时发送(聊天消息到另一个客户端)和接收(来自另一个客户端的聊天消息)?发生的情况是我总是发送数据但从不读
我有两个列表: List listA1 = GetFirstList(); List listA2 = GetSecondList(); 和A类定义 class A { public int
我使用了两个图像(img_heart_1 和 img_heart_2)。我有两种动画,一种是平移动画,另一种是缩放 text_anim.xml: animi(Animation),bounce_up.
考虑我有一个如下所示的运行方法,我正在尝试为 MyThread 创建四个线程。语句 1 到 3 可以由线程同时运行,也可以不同地运行。但是我想让Statement 4同时被线程执行。我可以在语句 4
对于新因素感到抱歉,但我正在阅读有关 mysql 的“太多连接”。 http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html m
我正在尝试自定义约束 菜单栏类: import UIKit class ManuBar: UIView { override init(frame: CGRect) { sup
我正在使用尚未为 Swift 4 做好准备的第 3 方库,即打破独占内存访问规则 ( SE-0176)。 因此,我的控制台充满了这些警告: Simultaneous accesses to 0xb1a
我需要调用一堆 *.exe,这样我就可以调用一个记事本文件,其中存储了所有要调用的 *.exe 的所有完整路径,并使用该记事本文件(可以编辑)我想创建一个调用列表的父 exe,其中存在所有子 *.ex
我正在尝试制作一个具有单个服务器和多个客户端场景的客户端服务器聊天应用程序。我的服务器正在工作,但我无法让客户端工作。我正在使用 select() 函数来选择我应该 recv() 还是 send()。
我正在尝试为类似 Pokemon 的游戏编写一个 AI,除了所有的 Action 都事先为两个玩家所知(所有玩家都可以看到)。我已经记录了我需要的所有信息,例如伤害、准确度、暴击率等。 我以为我的目标
假设账户ABC(fund:1000) 发生一笔6000 的取款交易,同时从ATM 取款8000。那么交易将如何管理。数据库是否只打开一个连接并且不允许新连接到同一帐户?? .... 在这种情况下,当交
我一直在尝试将视频(来自我的网络摄像头)同时输出到文件('out.mkv')和管道: 文件获取过滤帧,管道:获取未过滤的原始视频。 我的帧速率是 30 fps。但是,我的文件输出中的帧速率要低得多。
我们需要在一台PC上驱动8到12个显示器,所有显示器都呈现单个3D场景图的不同 View ,因此必须使用多个图形卡。我们目前正在dx9上运行,因此希望移至dx11,以期使此操作变得更容易。 初步调查似
我想以编程方式同时不在XML文件中显示两个动画,它应该 ROTATE和TRANSLATE 我怎样才能做到这一点? 请以某种方式建议我?????? 这是ma代码:> ImageView snowImg1
我是一名优秀的程序员,十分优秀!