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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
piotr
  • 21
  • 3
  • http://stackoverflow.com/questions/2327971/how-do-you-map-a-map-in-hibernate-using-annotations – Petar Minchev Jun 20 '10 at 16:14
  • don't know the answer, but a hint: both Vector and Hashtable are ancient. You should probably use ArrayList and HashMap, unless you need synchronization, in which case you should use classes from the java.util.concurrent package or wrap the collections in the Collections.synchronizedXyz() methods in java.util.Collections – Sean Patrick Floyd Jun 20 '10 at 19:33
  • @SeanPatrickFloyd But lookup of a hashtable or hashset is O(1) and allows quick lookup by an identifier whereas an arraylist or hashset requires iteration, O(n). In addition, hashtable is thread safe. – Nielsvh Apr 12 '17 at 15:09

1 Answers1

1

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
  • 562,542
  • 136
  • 1,062
  • 1,124