简体   繁体   中英

Legacy database mapping issue with JPA 1.0

I have a problem with mapping a legacy database with JPA 1.0. The database is stored denormalized for data-mining purposes. I condensed it to a simple example that hopefully clarifies the issue. Assume I have the following two tables:

Table ITEMS
  PKEY  GROUPID  NAME
  1     1        'AX'
  2     1        'AY'
  3     2        'BX'
  4     2        'BY'

Table XENTITY
  PKEY  ITEMGROUPID  NAME
  1     1            'E1'
  2     1            'E2'
  3     2            'E3'

What I basically want to accomplish is accessing all ITEMs from an XENTITY that have the GROUPID that is stored in the XENTITY's ITEMGROUPID column. Java-class-wise this should look like the following snippet.

class Item {
}

class XEntity {
    public Set<Item> getItems();
}

So for the XENTITIEs 'E1','E2' I whould get the ITEMs 'AX', 'AY' and for 'E3' I would get 'BX','BY'.

I am uncertain about how to map such a situation with JPA. Several trial-and-error attempts have unfortunately left me very empty-handed. I whould greatly appreciate any help in this regard.

Thanks in advance,

Alex

You have a very uncommon many-to-many association here, and I doubt you can get the information you want with a mapping. You should probably use a specific query to get what you want:

select item from Item item where item.groupId = :groupId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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