Hibernate persisting ArrayList - Issues

纵然是瞬间 提交于 2019-12-10 12:04:52

问题


Having some trouble getting this working. I am creating a site with a Tournament, which users can register for. The Tournament will have a list of registered members. It's not holding User objects, just the user email address, which is the a key for the user table anyway. I want to store the array list in such a way that I will have multiple copies of the tournament id, but only one instance of a username per tournament id.

eg T_ID Username 1 Tom 1 Mike 1 John 2 Tom 2 Chris 2 Timmy 3 Timmy 3 Chris

etc

Here's the SQL for the table.

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;

I have two classes as such.

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

The Register Class

    @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

I am using the proper javax.* annotations, not the Hibernate ones.

Current Error: Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on events.tournaments.Tournament.eligible references an unknown entity: java.util.List

I do have the Hibernate config set up to look in the package with the Register entity.

<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:


In your Register class, you are trying to map a list of strings as if they were entities:

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

@OneToMany is for relationships between entities only. Neither List nor String are entities. If you want to map a Collection of basic values (like String) to a table, take a look at the @CollectionTable annotation.




回答2:


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

    @Id
    @GeneratedValue
    private int id;

    @ElementCollection
    @CollectionTable (name = "tournament_eligible", joinColumns=@JoinColumn(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>();

I got rid of the Registered class. It was a bit superfluous and not really necessary for String objects.



来源:https://stackoverflow.com/questions/21379955/hibernate-persisting-arraylist-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!