没有交叉连接的JPA JOIN

时间:2016-01-22 10:37:24

标签: jpa join

我试图在下列表格之间建立关系

联合

@Entity
@NamedQuery(name="Combine.findAll", query="SELECT c FROM Combine c")
@Table(name="COMBINE")
public class Combine implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    private int artistid;

    private int automated;

    private int eventid;

    private int locationid;

    @Column(name="main_artist")
    private int mainArtist;

    public Combine() {
    }

事件

@Entity
@NamedQuery(name="Event.findAll", query="SELECT e FROM Event e")
@Table(name="event")
public class Event implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    private boolean active;

    private String addbyspider;

    private int automated;

    @Lob
    private String description;

    private Timestamp doorsopen;

    private Timestamp endtime;

    private Timestamp lastfm;

    @Column(name="latest_update")
    private Timestamp latestUpdate;

    private int minimumageid;

    private String name;

    private Timestamp nolatertime;

    private int podiuminfo;

    @Column(name="short_description")
    private String shortDescription;

    @Column(name="sold_out")
    private int soldOut;

    private Timestamp time;

    @Column(name="update_by_bot")
    private Timestamp updateByBot;

    /* TO DO: LINK NAAR EVENTPRICE */
//  public EventPrice getEventPrice() {
//      return eventPrice;
//  }
//
//  public void setEventPrice(EventPrice eventPrice) {
//      this.eventPrice = eventPrice;
//  }
//
//  @OneToOne(mappedBy = "event_price")
//  private EventPrice eventPrice;

    //bi-directional many-to-many association to Artist
    //@ManyToMany(fetch = FetchType.LAZY)
    @ManyToMany
    @JoinTable(
        name="COMBINE"
        , joinColumns={
            @JoinColumn(name="eventid")
            }
        , inverseJoinColumns={
            @JoinColumn(name="artistid")
            }
        )
    private List<Artist> artists;

    @OneToOne
    @JoinColumn(name="eventid")
    private List<Combine> combine;

    //bi-directional many-to-many association to Location
    //@ManyToMany(fetch = FetchType.EAGER)
    @OneToMany
    @JoinTable(
        name="COMBINE"
        , joinColumns={
            @JoinColumn(name="eventid")
            }
        , inverseJoinColumns={
            @JoinColumn(name="locationid")
            }
        )
    private List<Location> locations;

位置

@Entity
@NamedQuery(name="Location.findAll", query="SELECT l FROM Location l")
@Table(name="location")
public class Location implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    private boolean active;

    @Column(name="at_owner")
    private int atOwner;

    @Column(name="bad_ticket_site")
    private int badTicketSite;

    @Lob
    private String description;

    private int expectedBotItems;

    private int expectedBotRuntime;

    @Column(name="last_run")
    private Timestamp lastRun;

    private String name;

    private boolean notPrimaryBot;

    private int organisationid;

    private short priorityid;

    @Column(name="short_description")
    private String shortDescription;

    @Column(name="spider_by")
    private int spiderBy;

目前的做法是使用以下TypedQuery

生成与表的交叉连接
               StringBuffer sb = new StringBuffer();
sb.append("SELECT e FROM Event e, Combine c, Location l ");
sb.append("where e.id = c.eventid and l.id = c.locationid ");
sb.append("and e.time>=NOW() and e.soldOut='0' and e.active>=1");

这会生成

select event0_.id as id1_26_,
 event0_.active as active2_26_,
  event0_.addbyspider as addbyspi3_26_,
   event0_.automated as automate4_26_,
    event0_.description as descript5_26_,
     event0_.doorsopen as doorsope6_26_,
      event0_.endtime as endtime7_26_,
       event0_.lastfm as lastfm8_26_,
        event0_.latest_update as latest_u9_26_,
         event0_.minimumageid as minimum10_26_,
          event0_.name as name11_26_,
           event0_.nolatertime as nolater12_26_,
            event0_.podiuminfo as podiumi13_26_,
             event0_.short_description as short_d14_26_,
              event0_.sold_out as sold_ou15_26_,
               event0_.time as time16_26_,
                event0_.update_by_bot as update_17_26_
                 from
                  event event0_
                   cross join COMBINE combine1_
                    cross join location location2_
                    where event0_.id=combine1_.eventid and location2_.id=combine1_.locationid and event0_.time>=now() and event0_.sold_out='0' and event0_.active>=1 limit ?

我根本不想创建CROSS JOINS,而是希望表之间有INNER JOIN。我怎么能做到这一点?

OneToOne有问题,因为它没有构建到服务器上。任何人都知道为什么这不起作用?

@OneToOne
@JoinColumn(name="eventid")
private List<Combine> combine;

0 个答案:

没有答案