没有新实体的OneToMany关系

时间:2012-04-27 12:08:47

标签: java hibernate jpa one-to-many

使用JPA和Hibernate
我有实体类

@Entity
@Table(name = "ROLES")
public class Role implements Serializable 
{

   @Column
   private List<String> tubes;    

// another fields .setters, getters  
}  

列表中的每个字符串 - 是另一个表(TUBES)中的一行。 表格ROLES与TUBES有OneToMany关系 A可以在实体管上创建另一个实体并映射表TUBES。但是如何在没有其他实体的情况下实现这一点

修改:

我做了

  @ElementCollection
   @CollectionTable(name = "TUBES", joinColumns = @JoinColumn(name = "role_id"))
   @Column(name = "tube")
   private ArrayList<String> tubes;  

部署在JBoss上。在rintime中我得到了SQLGrammarException

JPA创建的查询是:

/* SELECT r FROM Role r WHERE r.name = ? */ select role0_.AA_ID as AA1_0_, role0_.ROLNAME as ROLNAME0_, role0_.role as PID4_0_ from PIDDB_PID_ROLE role0_ where role0_.ROLNAME=?

17:17:14,661 ERROR [JDBCExceptionReporter] ORA-00904: "ROLE0_"."tube": invalid identifier

1 个答案:

答案 0 :(得分:3)

您可以使用@ElementCollection映射我认为这就是您要寻找的内容。

@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="TUBES", joinColumns=@JoinColumn(name="role_id"))
@Column(name="tube")
private List<String> tubes;    

<强>更新

dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.0.Final</version>
</dependency>

**Update2:**

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;