简体   繁体   English

映射多对一外键Java EE

[英]Mapping many-to-one foreign key Java EE

This is homework and I don't understand so help is appreciated. 这是家庭作业,我不明白,所以感谢帮助。 The system is an Olympic medal tally simulator. 该系统是奥运奖牌计数模拟器。

We have an Events table and a Countries table: 我们有一个Events表和一个Countries表:

statement.execute("create table EVENTS (EVENTNUMBER integer primary key not null, EVENTNAME varchar(32), " +
               " GENDER varchar(5), GOLD integer, SILVER integer, BRONZE integer)");

statement.execute("create table COUNTRIES (COUNTRYID integer primary key not null, NAME varchar(32)," +
                       "TOTALGOLD integer, TOTALSILVER integer, TOTALBRONZE integer)");

These relate to entities: 这些与实体有关:

@Entity
@Table(name = "COUNTRIES")
public class Country implements Serializable
{

   @Id
   private int countryID; // This is the primary key
   private String name;
   private int totalGold;
   private int totalSilver;
   private int totalBronze;
//getters, setters omitted

@Entity
@Table(name = "EVENTS")
public class Event implements Serializable
{

    @Id
    private int eventNumber;     // This is the primary key
    private String eventName;
    private String gender;       // Men, Women or Mixed
    @ManyToOne(optional=false)
    @JoinColumn(name="COUNTRYID", nullable=false, updatable=false)
    private Country gold, silver, bronze;

//getter, setter methods omitted

The question says to add three Country instance variables to Event to represent the gold, silver and bronze winners and include the relationship type. 问题是向Event添加三个Country实例变量来表示金牌,银牌和铜牌,并包括关系类型。 To me it makes sense to be a many-to-one. 对我来说,做多对一是有意义的。 But, when I try run this I get an error about join being wrong because it should allow only one write and the rest as read only. 但是,当我尝试运行这个时,我得到一个关于连接错误的错误,因为它应该只允许一次写入,其余的只读。

So, I changed the JoinColumn to insertable=false and get an error: Column 'COUNTRYID' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. 因此,我将JoinColumn更改为insertable = false并获得错误:列'COUNTRYID'要么不在FROM列表中的任何表中,要么出现在连接规范中,并且超出了连接规范的范围或出现在HAVING子句中并且不在GROUP BY列表中。

Which is hit at this method in my QueriesBean: 在我的QueriesBean中遇到了这个方法:

   public List<Event> getEvents()
   {
      List<Event> eventList = null;

      Query query = em.createQuery("SELECT e FROM Event e");
      eventList = (List<Event>) query.getResultList();

      return eventList;
   }

Perhaps someone can give me some guidance? 也许有人可以给我一些指导?

@JoinColumn(name="COUNTRYID", nullable=false, updatable=false)
private Country gold;

The above line means: There is an association between this entity (Event) and the Country entity, and this association is materialized by the column "COUNTRID" in the table EVENTS. 上述行表示:此实体(Event)与Country实体之间存在关联,此关联由EVENTS表中的“COUNTRID”列实现。

There is already a problem: you don't have any COUNTRYID column in the EVENTS table. 已存在问题:EVENTS表中没有任何COUNTRYID列。

@JoinColumn(name="COUNTRYID", nullable=false, updatable=false)
private Country gold, silver, bronze;

The above line means: there are three different associations between this entity (Event) and the Country entity, and all these associations are materialized by the same column COUNTRYID. 上述行意味着:此实体(Event)与Country实体之间存在三种不同的关联,所有这些关联都由同一列COUNTRYID实现。 This is of course impossible. 这当然是不可能的。 If you have three associations, you need three different columns. 如果您有三个关联,则需要三个不同的列。

A good rule, whether you have annotations or not, is to always have one declaration per line. 无论您是否注释,一个好的规则是每行始终有一个声明。 Declare your three associations in three separate lines, and annotate each of them with the right annotations. 在三个单独的行中声明您的三个关联,并使用正确的注释注释每个关联。 Each of them is a ManyToOne. 他们每个人都是ManyToOne。 And each of them has its own column to materialize the association. 每个人都有自己的专栏来实现这种联系。 And of course this column must exist in the table. 当然,该列必须存在于表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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