休眠坚持ArrayList的 - 问题 [英] Hibernate persisting ArrayList - Issues
问题描述
有一些麻烦这个工作。我创建了比赛,哪些用户可以注册一个网站。本次比赛将有注册会员的名单。这不是拿着用户对象,只是用户的电子邮件地址,这是user表中的一个关键反正。我想存储在这样一种方式,我将有比赛的id的多个副本的数组列表,但只有一个每比赛ID的用户名的实例
如
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的;
- ------------------------------------------------ -----
- 表`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`)
约束`fk_tournament_eligible_tournament1`
外键(`id`)
参考`mtc`.`tournament`(`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB的;
我有两个阶级作为阶级。
Tournament.java
@Entity
@Table(NAME =赛)
公共类比赛实现事件{ @ID
@GeneratedValue
私人诠释身份证; @OneToMany
@JoinColumn(NAME =ID)
私人列表<使用者>合格; //包含符合条件的成员列表 @Size(分= 5,上限= 45,消息=赛事名称必须在5至60个字符,组= {
PersistenceValidationGroup.class,FormValidationGroup.class})
私人字符串tournamentName; //指定比赛的名称 私人字符串tournamentGender =混合; //指定在一个巡回赛
//为M(ALE),F(emale)或混合; 私人字符串tournamentType =S; //指定S(英格尔)或D(oubles) 私人字符串tournamentCategory =O; //指定MEMBER_TYPE是
// S(enior)只,J(UNIOR)只,
//或O(笔)锦标赛 私人字符串tournamentStyle =L; // Specfies类型锦标赛是
//(L)的加法器/(L)eague,(B)ucket或
//(G)roup - 可能将其更改为
//类以后。 公开锦标赛(){
this.eligible =新的ArrayList<使用者>();
}// getter和setter
寄存器类别
@Entity
@Table(NAME =tournament_eligible)
公共类注册{
@ID
@Column(NAME =ID)
私人诠释tournamentID; @Column(名称=用户名)
私人字符串用户名; @OneToMany(的mappedBy =赛)
私人列表<串GT;注册;// getter和setter方法
我是使用正确的javax。*注释,而不是休眠的。
电流误差:因:org.hibernate.AnnotationException:上events.tournaments.Tournament.eligible @OneToOne或@ManyToOne引用了未知的实体:java.util.List的
我有Hibernate的配置设置在封装与注册实体一下。
<豆类配置=生产>
< JEE:JNDI的查找JNDI名称=的jdbc / MTCID =数据源
预计-TYPE =javax.sql.DataSource中的>
< / JEE:JNDI的查找>
<豆的id =transactionManager的
类=org.springframework.jdbc.datasource.DataSourceTransactionManager>
<属性名=数据源REF =数据源>< /性>
< /豆>
< TX:注解驱动/>
<豆的id =SessionFactory的
类=org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
<属性名=数据源REF =数据源>< /性>
<属性名=hibernateProperties>
<道具及GT;
<支撑键=hibernate.dialect> org.hibernate.dialect.MySQL5Dialect< /道具>
< /道具>
< /性>
<属性名=packagesToScan>
<列表>
< VALUE>用户< /值>
< VALUE>&DAO LT; /值>
< VALUE> events.tournaments< /值>
< /列表>
< /性>
< /豆>
<豆的id =exceptionTranslator
类=org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor>
< /豆>
< /豆>
在你的注册
类,你试图映射,好像他们是实体的字符串列表:
@OneToMany(的mappedBy =赛)
私人列表<串GT;注册;
@OneToMany
是唯一的实体之间的关系。无论是列表
也不字符串
都是实体。如果你想一个的收藏
基本价值(如字符串
)映射到一个表,看看在< A HREF =http://docs.oracle.com/javaee/6/api/javax/persistence/CollectionTable.html相对=nofollow> @CollectionTable
注释。
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>
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.
这篇关于休眠坚持ArrayList的 - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!