Hibernate 持久化 ArrayList - 问题 [英] Hibernate persisting ArrayList - Issues
问题描述
在使用此功能时遇到一些麻烦.我正在创建一个带有锦标赛的网站,用户可以注册.比赛将有一个注册会员名单.它不保存 User 对象,只保存用户电子邮件地址,无论如何它都是用户表的键.我想以这样一种方式存储数组列表,即我将拥有锦标赛 ID 的多个副本,但每个锦标赛 ID 只有一个用户名实例.
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.
例如T_ID 用户名1 汤姆1 迈克1 约翰2 汤姆2 克里斯2 提米3 提米3克里斯
eg T_ID Username 1 Tom 1 Mike 1 John 2 Tom 2 Chris 2 Timmy 3 Timmy 3 Chris
等
这是表的 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;
我有两个这样的课程.
锦标赛.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.* 注释,而不是 Hibernate 注释.
I am using the proper javax.* annotations, not the Hibernate ones.
当前错误:由:org.hibernate.AnnotationException:@OneToOne 或 @ManyToOne on events.tournaments.Tournament.eligible 引用未知实体:java.util.List
Current Error: Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on events.tournaments.Tournament.eligible references an unknown entity: java.util.List
我确实设置了 Hibernate 配置以查看包含注册实体的包.
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>
推荐答案
在您的 Register
类中,您试图将字符串列表映射为实体:
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
仅用于实体之间的关系.List
和 String
都不是实体.如果要将基本值(如 String
)的 Collection
映射到表,请查看 @CollectionTable
注释.
@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.
这篇关于Hibernate 持久化 ArrayList - 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!