Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Many to many relation

luanlopes94 August 22, 2018

Hi,

I have two entities with a many to many relation. First entity is Carrier and the second entity is Folder.

public interface Folder extends Entity {

// other methods

@ManyToMany(through = "getFolder", reverse = "getCarrier", value = CarrierToFolder.class)
Carrier[] getCarriers();
public interface Carrier extends Entity {

// others methods

@ManyToMany(through = "getCarrier", reverse = "getFolder", value = CarrierToFolder.class)
public Folder[] getFolders();

}

How is the correct way to recovery all carriers associated with a folder?

I'm trying with this:

@Override
public Folder getFolder(String folderName) {
try {
Folder[] folder = ao.find(Folder.class,
Query.select().where("FOLDER_NAME = ?", folderName ));

if (folder.length != 0) {
return folder[0];
} else {
log.error("The query has not results.");
return null;
}
} catch (Exception ex) {
log.error("Error on get folder: " + ex.getMessage());
}
return null;
}

But, folder[0].getCarriers() always return 01 element.

Thank you.

1 answer

0 votes
luanlopes94 August 23, 2018

With inner join example:

Carrier[] carriers = ao.find(Carrier.class,
Query.select()
.alias(Carrier.class, "table1")
.alias(CarrierToFolder.class, "table2")
.join(CarrierToFolder.class, "table1.ID = table2.ID")
.where("table2.ID = ?", folders[0].getID())
);

The array only stay with 1 element. Is there a query limit or restriction?

Suggest an answer

Log in or Sign up to answer