On Jan 18, 2014 8:28 AM, <waldem...@gmail.com> wrote:
>
> I feel like it is not...
>
> I'm trying for a week now how I can join my table A with table B via a join table A_B.
A single query joining three tables is not necessarily the most efficient way to build an object graph, the primary factors being width of the entities (number and size of fields on each side) and cardinality. A good ORM will let you control the method, though. Have a look at FetchConfig to see how to do this.
> Btw. I got this thing to work with a @ManyToMany mapping, but my join table has an additional field, which I need to order the result.
You'll likely need an entity for the join table then.
/D
On Jan 18, 2014 10:50 AM, "Daryl Stultz" <kungfum...@gmail.com> wrote:
>
> > Btw. I got this thing to work with a @ManyToMany mapping, but my join table has an additional field, which I need to order the result.
>
> You'll likely need an entity for the join table then.
Actually the ordering thing is going to be a lot more challenging than just modelling the join table. It's easy enough to sort a child collection on an internal property, not sure if you can do that on a property on a different entity.
>
> /D
.fetch("articles_authors", "full_name", new FetchConfig().query())Publication.class
Entity
@Table(name="articles")
public class Publication extends Model {
@Id
public Long id;
@OneToMany(cascade = CascadeType.ALL,mappedBy="articles")
public List<Articles_Authors> articles_authors = new ArrayList<Articles_Authors>();
}
Authors.java
@Entity
@Table(name="authors")
public class Author extends Model {
@Id
public Long id;
@OneToMany(cascade = CascadeType.ALL,mappedBy="authors")
public List<Articles_Authors> articles_authors = new ArrayList<Articles_Authors>();
}
Articles_Authors.java
@Entity
@Table(name="articles_authors")
public class Articles_Authors extends Model {
@Id
public Long id;
@ManyToOne
public Publication articles;
@ManyToOne
public Author authors;
}
Here is the find statement
Page<Publication> pageList = find.where()
.ilike(searchField, "%" + filter + "%")
.orderBy(sortBy + " " + order)
.fetch("articles_authors.authors", new FetchConfig().query(3).lazy(10))
.findPagingList(pageSize)
.setFetchAhead(false)
.getPage(page);Authors.java
@Entity @Table(name="authors") public class Author extends Model { @Id public Long id; @OneToMany(cascade = CascadeType.ALL,mappedBy="authors") public List<Articles_Authors> articles_authors = new ArrayList<Articles_Authors>(); }
On the website I can access the authors_id, but how can I get the full_name?
I'm somewhat confused by your mix of singular and plural entity names. You use both Authors and Author as class names, for example. Is this real code?Authors.java
@Entity @Table(name="authors") public class Author extends Model { @Id public Long id; @OneToMany(cascade = CascadeType.ALL,mappedBy="authors") public List<Articles_Authors> articles_authors = new ArrayList<Articles_Authors>(); }
On the website I can access the authors_id, but how can I get the full_name?
I don't see full_name defined anywhere (it does not appear to be a property that you can get). Do you need to define it? What does the Authors database table look like?
Authors.java
@Entity @Table(name="authors") public class Author extends Model {
@Id public Long id; @OneToMany(cascade = CascadeType.ALL,mappedBy="authors") public List<Articles_Authors> articles_authors = new ArrayList<Articles_Authors>();
}