tags:

views:

35

answers:

1

I have this class:

public class DBRow {
    public String url;
    public String title;
    public static Hashtable<String, Integer> words;
    public Vector<String> keywords;
}

What I must do is store many instances of this class in a database. I'm using Hibernate and JPA to get the job done. Using JPA I've come so far (really not far):

@Entity
@Table(name="MAIN_TABLE")
public class DBRow {

    @Column(name="url") 
    public String url;
    @Column(name="title")
    public String title;
    public static Hashtable<String, Integer> words;
    public Vector<String> keywords;

}

I want my database to have 3 tables - MAIN_TABLE - auto-icremented id as primary key, url and title; HASH - containing a key-value pair from the Hashtable<String, Integer> and id to refer to which instance of DBRow class it belongs (and also to relate to the MAIN_TABLE); VECTOR - pretty much the same story like HASH but with a Vector<String>. So what I'm asking is hot to map the hashtable and the vector, using JPA to get it done?? I've been trying to find a way to do this but haven't found one so far... or maybe I'm not on the right way! Any suggestions are welcome. Thank you in advance.

+1  A: 

This is not possible with JPA 1.0 (at least, not with standard annotations) and since you did not mention your JPA provider, I will only cover JPA 2.0. In JPA 2.0, the @ElementCollection annotation can be used to map a collection of basic types or embeddable objects.

Below some examples taken from EclipseLink/Development/JPA 2.0/new collection mappings:

Example 1 - An element collection representing a basic collection mapping.

@ElementCollection
@Column(name="DESIGNATION")
// CollectionTable will default in this case, Entity name + "_" + attribute name
// JoinColumns will default in this case which are different from BasicCollection collection table default
private Collection<String> designations;

Example 2 - An element collection representing an basic map mapping.

@ElementCollection
@MapKeyColumn(name="Q_DATE")
@Column(name="QUOTE")
@CollectionTable(name="EXPERT_QUOTES", joinColumns=@JoinColumn(name="EBC_ID"))
public Map<Date, String> getQuotes() {
    return quotes;
}

Now, I second one of the comment to your question and I would consider using "moderner" data structure like ArrayList and HashMap.

Also note that static (and final) entity fields are always considered to be transient i.e. can't be persisted.

Pascal Thivent