没有可用外键的 JPA 实体映射
我并没有真正开始使用实体 bean,但它们现在正盯着我看。
我们有几个相互关联的表,但没有外键,也无法添加任何外键。 对于这个问题,数据库不能改变,这是不可能的,但作为不同的解决方案,我们可以创建视图。 不管怎样...
我有3张桌子。 LOCATION 、 LINKS 和 ENDPOINT ,为了额外的盐,我们有一个 LINKS_TYPE 表。
LOCATION 表有一个主键 LOCATIONID,它是包含位置 id 的字符串。
LINKS 表有一个 LINKID 作为主键 包含 ENDPOINT 主键的列 LINK_ATTR_VALUE_A 包含 LOCATION 主键的 LINK_ATTR_VALUE_B 列。 包含 LINKS_TYPE 主键的 LINKTYPEID 列
ENDPOINT 表具有主键 POINTID
LINKS_TYPE 具有主键 LINKTYPEID LINKTYPEA 列(定义其链接到的表名称的文本字符串) LINKTYPEB 列(定义其链接到的表名的文本字符串)
现在,即使提到了 LINKS_TYPE,我现在也不需要担心这一点,因为在此数据库实例中没有其他链接。
我想在我的 LOCATION 实体“List endPoints”中定义一个成员 根据我的理解,这将是一个@OneToMany。 请记住,这里没有外键可以提供帮助,而且永远不会有。
这是我定义的映射...
@OneToMany ( cascade=CascadeType.ALL)
@JoinTable ( name = "ENDPOINT",
joinColumns = @JoinColumn (
name = "LINK_ATTR_VALUE_B"
),
inverseJoinColumns =
@JoinColumn (
name = "LINK_ATTR_VALUE_A"
)
)
private List<EndPoint> endPoints;
很可能从中您可能会意识到我不知道我在做什么:D 但是文档并不太好,我已经订购了一本关于 ejb 3 的帮助书,但我只是没有时间在这个映射之前完成这本书:D
我们正在使用 TopLink 和 jdeveloper 11g 以及 weblogic 服务器和 oracle 10g 作为数据库。
当使用 serviceFacade 客户端进行查询时,一切似乎都正确(从那以后工作区消失了,我必须重新创建项目才能让客户端工作)。 在我看来,它生成了完美的查询来检索正确的数据。 然而,最终还是没有结果。
我愿意提供尽可能多的信息,只是不确定需要什么。 但我知道我的映射很可能是错误的,这是因为我不理解映射。
有人可以帮助我吗?
谢谢。
I dont really get to work with entity beans, but they are staring at me right now.
We have a couple of tables that are related to each other, but there are no foreign keys, and we cannot add any. For this question, the database cannot change, it's just not possible, but as a different solution we might create views. Anyways...
I have 3 tables.
LOCATION , LINKS and ENDPOINT and for extra salt, we a a LINKS_TYPE table.
LOCATION table has a primary key LOCATIONID which is a string containing the location id.
LINKS table has a LINKID as primary key
a column LINK_ATTR_VALUE_A which contains ENDPOINT's primary key
a column LINK_ATTR_VALUE_B which contains LOCATION's primary key.
a column LINKTYPEID which contains a LINKS_TYPE primary key
ENDPOINT table has a primary key POINTID
LINKS_TYPE has primary key LINKTYPEID
a column LINKTYPEA (text string defining the table name it's linked to)
a column LINKTYPEB (text string defining the table name it's linked to)
Now even though LINKS_TYPE is mentioned, I dont need to worry about that now, because there are no other links in this instance of the database.
I would like to define a member in my LOCATION entity 'List endPoints'
Which would be a @OneToMany from my understanding.
Do keep in mind, there is no foreign key to help here, and there wont ever be.
This is the mapping I defined...
@OneToMany ( cascade=CascadeType.ALL)
@JoinTable ( name = "ENDPOINT",
joinColumns = @JoinColumn (
name = "LINK_ATTR_VALUE_B"
),
inverseJoinColumns =
@JoinColumn (
name = "LINK_ATTR_VALUE_A"
)
)
private List<EndPoint> endPoints;
It's very likely from this you might realize I have no clue what I'm doing :D
But the documentation aint too great, and Ive ordered a book for help with ejb 3, but I just dont have the time to finish the book before this mapping :D
We are using TopLink with jdeveloper 11g and that weblogic server thing and oracle 10g as database.
When using a serviceFacade client to query, everything did seem right (since then workspace died and I have to recreate the project to get the client working).
It generates the perfect query in my opinion to retrieve the right data.
Yet, it ends with no results.
I'm willing to give as much info as possible, just not sure what is needed.
But I know my mapping is most probably wrong, and it's because I do not understand the mapping.
Could someone help me?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 LINKS 表看起来像是 LOCATION 和 ENDPOINT 之间的多对多映射表,而不是一对多。 这里的一个大问题是,除了您列出的
LINKID
、LINK_ATTR_VALUE_A
和LINK_ATTR_VALUE_B
之外,它是否还有其他列?如果是这样,那么您必须将其映射为单独的实体:
Location
将具有映射为双向一对多的Links
集合Link
将与Location
和EndPoint
建立多对一关系如果 OTOH,LINKS 没有其他关系列 AND 您愿意放弃其主键(对于多对多连接表来说既不需要也不能映射),那么您可以将其映射为 < 的多对多集合
位置
上的code>端点。如果您可以澄清您的问题,我将更新我的答案以包含实际的映射(如果您需要)。
Your LINKS table looks like many-to-many mapping table between LOCATION and ENDPOINT rather than one-to-many. The big question here is whether it has any additional columns aside from
LINKID
,LINK_ATTR_VALUE_A
andLINK_ATTR_VALUE_B
that you listed?If it does, then you would have to map it as a separate entity:
Location
would have a collection ofLinks
mapped as bi-directional one-to-manyLink
would have many-to-one relationships with bothLocation
andEndPoint
If, OTOH, LINKS has no other columns AND you're willing to forgo its primary key (which is neither needed nor can be mapped for many-to-many join table) then you can map it as many-to-many collection of
EndPoint
s onLocation
.If you can clarify your question I'll update my answer to include the actual mapping if you need it.
这是我结束时的映射。
LINK 表中目前并没有真正需要的任何值。
但到了时候,我们的 DBA 将需要为我们创建物化视图或其他东西。
但在尝试映射时,我最初保留了LINK,而不是直接前往endPoint。 我被返回了 5000 多个链接,而应该只有 133 个。因此,又出现了一个我不理解的映射,但我将其留到以后再处理。
目前我们的数据库仅包含 1 种链接类型。 这将会改变,我确实有一种方法可以为映射添加一个额外的 where 子句,这样我就可以为不同的类型拥有不同的属性映射。
我今天有打字的心情 :-D
Here is the mapping I ended on.
There just isnt truly any values needed right now in the LINK table.
But when it' time, our DBA's will need to create materialized views for us or something.
But when attemting the mapping, I initialy kept the LINK, rather than going straight to the endPoint. I was being returned 5000+ links where there should only be 133. So again there is a mapping I dont understand,but I'll leave that for later.
At the moment our database only contains 1 link type. This will change, and I really which there was a way for me to add an additional where clause to the mapping, so I could have different attribute mappings for different types.
I'm in a typing mood today :-D