简体   繁体   中英

JPA: Retrieve a list from secondary table using a composite key

well, here I have an example of what I'm trying to do with no success. Sorry for the dummy example. I have the following tables:

CREATE TABLE HOUSE(
  HOUSE_NAME VARCHAR2(255),
  OWNER_ID VARCHAR2(255),
  PRIMARY KEY(FOLDER_NAME, USER_ID)
);

CREATE TABLE PET(
  HOUSE_NAME VARCHAR2(255) NOT NULL, 
  OWNER_ID VARCHAR2(255) NOT NULL,
  PET_NAME VARCHAR2(255) NOT NULL
);

ALTER TABLE PET
  ADD CONSTRAINT FK_PET_HOUSE
    FOREIGN KEY(HOUSE_NAME, OWNER_ID)
    REFERENCES HOUSE(HOUSE_NAME, OWNER_ID)
    ON DELETE CASCADE;

INSERT INTO HOUSE VALUES('House 1', 'Owner 1');
INSERT INTO HOUSE VALUES('House 2', 'Owner 1');
INSERT INTO HOUSE VALUES('House 3', 'Owner 1');

INSERT INTO PET VALUES('House 1', 'Owner 1', 'Dog');
INSERT INTO PET VALUES('House 1', 'Owner 1', 'Cat');
INSERT INTO PET VALUES('House 2', 'Owner 1', 'Duck');
INSERT INTO PET VALUES('House 3', 'Owner 1', 'Bird');

As you can see I'm using a composite key in my "HOUSE" table. What I want is to get from DB are 3 objects something like this:

Owner Id: Owner1
House name: House 1
Pets: [Dog, Cat]

Owner Id: Owner1
House name: House 2
Pets: [Duck]

Owner Id: Owner1
House name: House 3
Pets: [Bird]

I've been searching for a way to do it but I don't know how to link these tables to retrieve the list. What I have so far is:

@Entity
@Table(name="HOUSE")
@SecondaryTable(name="PET", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="HOUSE_NAME", referencedColumnName="HOUSE_NAME"),
        @PrimaryKeyJoinColumn(name="OWNER_ID", referencedColumnName="OWNER_ID"),
})
public class HousePets implements Serializable {

    @Id
    @Column(name="HOUSE_NAME")
    private String houseName;

    @Id
    @Column(name="OWNER_ID")
    private String ownerId;

    @Column(table="PET" name="PET_NAME") //DON'T REALLY KNOW HOW TO DEAL WITH IT
    private Set<String> petsSet = new HashSet<String>

    //GETTERS AND SETTERS
}

And to retrieve the list I'm using a method something like this:

    public List<HousePets> getFoldersList() {
        em = emf.createEntityManager();
        Query q = em.createQuery("SELECT h FROM HousePets h WHERE h.ownerId = :ownerId");
        q.setParameter("ownerId", "Owner 1");
        List<HousePets> result = q.getResultList();
        return result;
    }

Thanks for your time!

Your model makes no sense. A SecondaryTable cannot be a OneToMany relation.

What you need is an OWNER table, that defines the OWNER_ID, (maybe a name, etc.)

Owner would have a OneToMany to Pet, and OneToOne (or ManyToOne, OneToMany?) to House

Pet would have a ManyToOne to Owner and ManyToOne to House

House would have a OneToMany to Pet and OneToMany? to Owner

To get an own, you would just select the owner, to get their pets you would just access them.

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