Ilya
Ilya

Reputation: 29693

OneToMany relationship without new entity

Using JPA and Hibernate
I have an entity class

@Entity
@Table(name = "ROLES")
public class Role implements Serializable 
{
       
   @Column
   private List<String> tubes;    

// another fields .setters, getters  
}  

Every String in List tubes - is one row from another table (TUBES).

Table ROLES has an OneToMany relationship with TUBES.
A can make another Entity Tubes and map table TUBES on Entity Tubes. But how I can make this without another entity?

Edit:

I made

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

Deploy on JBoss. And in runtime I get SQLGrammarException

Query created by JPA is:

/* 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

Upvotes: 1

Views: 1439

Answers (1)

danny.lesnik
danny.lesnik

Reputation: 18639

You can use @ElementCollection mapping I think this is what are you looking for.

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

Update:

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;

Upvotes: 3

Related Questions