2014-01-27 100 views
0

有一些麻煩得到這個工作。我正在創建一個有錦標賽的網站,用戶可以註冊。錦標賽將有一個註冊會員名單。它沒有保存用戶對象,只是用戶的電子郵件地址,無論如何這都是用戶表的關鍵。我想以這樣一種方式存儲數組列表,我將有多個比賽ID副本,但每個比賽ID只有一個用戶名實例。Hibernate持久化ArrayList - 問題

如 T_ID用戶名 1湯姆 1邁克 約翰 2湯姆 2克里斯 2提米 3提米 3克里斯

這裏的表的SQL。

DROP TABLE IF EXISTS `mtc`.`tournament` ; 

CREATE TABLE IF NOT EXISTS `mtc`.`tournament` (
    `id` INT NOT NULL, 
    `tournamentName` VARCHAR(100) NULL, 
    `tournamentGender` VARCHAR(45) NULL, 
    `tournamentType` VARCHAR(45) NULL, 
    `tournamentCategory` VARCHAR(45) NULL, 
    `tournamentStyle` VARCHAR(45) NULL, 
    PRIMARY KEY (`id`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mtc`.`tournament_eligible` 
-- ----------------------------------------------------- 
DROP TABLE IF EXISTS `mtc`.`tournament_eligible` ; 

CREATE TABLE IF NOT EXISTS `mtc`.`tournament_eligible` (
    `id` INT NOT NULL, 
    `username` VARCHAR(45) NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `fk_tournament_eligible_tournament1` 
    FOREIGN KEY (`id`) 
    REFERENCES `mtc`.`tournament` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 

我有兩個類。

Tournament.java

@Entity 
    @Table(name = "tournament") 
    public class Tournament implements Event { 

     @Id 
     @GeneratedValue 
     private int id; 

     @OneToMany 
     @JoinColumn(name = "id") 
     private List<User> eligible; // Contains a list of eligible members 

     @Size(min = 5, max = 45, message = "Tournament Name must be between 5 and 60 characters", groups = { 
       PersistenceValidationGroup.class, FormValidationGroup.class }) 
     private String tournamentName; // Specified the name of the tournament 

     private String tournamentGender = "MIXED"; // Specifies where a tournament 
                // is M(ale), F(emale) or MIXED; 

     private String tournamentType = "S"; // Specifies S(ingles) or D(oubles) 

     private String tournamentCategory = "O"; // Specifies Member_Type to be 
                // S(enior) only, J(unior) only, 
                // or O(pen) Tournament 

     private String tournamentStyle = "L"; // Specfies type of Tournament to be 
               // (L)adder/(L)eague, (B)ucket or 
               // (G)roup - Probably change this to 
               // a class later on. 

     public Tournament() { 
      this.eligible = new ArrayList<User>(); 
     } 

// getters and setters 

的註冊類

@Entity 
    @Table(name="tournament_eligible") 
    public class Registered { 


     @Id 
     @Column(name="id") 
     private int tournamentID; 

     @Column(name="username") 
     private String username; 

     @OneToMany(mappedBy="tournament") 
     private List<String> registered; 

// getter and setters 

我使用的是正確的javax。*註釋,而不是休眠的。

電流誤差:org.hibernate.AnnotationException:由造成了events.tournaments.Tournament.eligible @OneToOne或@ManyToOne引用了未知的實體:java.util.List的

我有Hibernate的配置集直到查看包含Register實體的包。

<beans profile="production"> 
     <jee:jndi-lookup jndi-name="jdbc/mtc" id="dataSource" 
      expected-type="javax.sql.DataSource"> 
     </jee:jndi-lookup> 
     <bean id="transactionManager" 
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
      <property name="dataSource" ref="dataSource"></property> 
     </bean> 
     <tx:annotation-driven /> 
     <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
      <property name="dataSource" ref="dataSource"></property> 
      <property name="hibernateProperties"> 
       <props> 
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
       </props> 
      </property> 
      <property name="packagesToScan"> 
       <list> 
        <value>users</value> 
        <value>dao</value> 
        <value>events.tournaments</value> 
       </list> 
      </property> 
     </bean> 
     <bean id="exceptionTranslator" 
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"> 
     </bean> 
    </beans> 

回答

1

在你Register類,你想,如果他們的實體映射字符串列表:

@OneToMany(mappedBy="tournament") 
    private List<String> registered; 

@OneToMany僅用於實體之間的關係。 ListString都不是實體。如果要將基本值(如String)的Collection映射到表中,請查看@CollectionTable annotation

+0

太棒了!這似乎是爲我整理出來的。我現在要發佈工人階級。我可能會在以後再打破它! :) –

+0

酷,很高興你得到它的工作:) – kostja

2
@Entity 
@Table(name="tournament") 
public class Tournament implements Event { 

    @Id 
    @GeneratedValue 
    private int id; 

    @ElementCollection 
    @CollectionTable (name = "tournament_eligible", [email protected](name="id")) 
    private List<String> username; // Contains a list of eligible members 

    @Size(min=5, max=45, message="Tournament Name must be between 5 and 60 characters",groups={PersistenceValidationGroup.class, FormValidationGroup.class}) 
    private String tournamentName; // Specified the name of the tournament 

    private String tournamentGender = "MIXED"; // Specifies where a tournament is M(ale), F(emale) or MIXED; 

    private String tournamentType = "S"; // Specifies S(ingles) or D(oubles) 

    private String tournamentCategory = "O"; // Specifies Member_Type to be S(enior) only, J(unior) only, or O(pen) Tournament 

    private String tournamentStyle = "L"; // Specfies type of Tournament to be (L)adder/(L)eague, (B)ucket or (G)roup - Probably change this to a class later on. 

    public Tournament(){ 
     this.username = new ArrayList<String>(); 

我擺脫了註冊類。這對於String對象來說有點多餘而且沒有必要。