Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA Mapping Multi-Rows with ElementCollection
icon3.gif  JPA Mapping Multi-Rows with ElementCollection [message #1064961] Sat, 22 June 2013 01:54 Go to next message
Yang Chen is currently offline Yang ChenFriend
Messages: 1
Registered: June 2013
Junior Member
I'm trying to follow the JPA tutorial (en.wikibooks.org/wiki/Java_Persistence/ElementCollection#Example_of_an_ElementCollection_relationship_database) and using ElementCollection to record employee phone numbers:

PHONE (table)
OWNER_ID    TYPE      NUMBER
  1         home      792-0001
  1         work      494-1234
  2         work      892-0005


Short version

What I need is a class like this:

@Entity
@Table(name="Phones")
  public class PhoneId {
  @Id
  @Column(name="owner_id")
  long owner_id;

  @Embedded
  List<Phone> phones;
}

that stores each person's phone numbers in a collection.

Long version

I follow the tutorial code:

@Entity
@Table(name="Phones")
public class PhoneId {
  @Id
  @Column(name="owner_id")
  long owner_id;

  @ElementCollection
  @CollectionTable(
    name="Phones",
    joinColumns=@JoinColumn(name="owner_id")
  )
  List<Phone> phones = new ArrayList<Phone>();
}

@Embeddable
class Phone {
  @Column(name="type")
  String type = "";
  @Column(name="number")
  String number = "";

  public Phone () {}
  public Phone (String type, String number)
    { this.type = type; this.number = number; }
}

with a slight difference that I only keep one table. I tried to use the following code to add records to this table:

public static void main (String[] args) {
  EntityManagerFactory entityFactory =
     Persistence.createEntityManagerFactory("Tutorial");
  EntityManager entityManager = entityFactory.createEntityManager();

  // Create new entity
  entityManager.getTransaction().begin();
  Phone ph = new Phone("home", "001-010-0100");
  PhoneId phid = new PhoneId();
  phid.phones.add(ph);
  entityManager.persist(phid);
  entityManager.getTransaction().commit();

  entityManager.close();
}

but it keeps throwing exceptions

Quote:
Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "type" violates not-null constraint Detail: Failing row contains (0, null, null). Error Code: 0 Call: INSERT INTO Phones (owner_id) VALUES (?) bind => [1 parameter bound] Query: InsertObjectQuery(tutorial.Phone1@162e295)

What did I do wrong?
Re: JPA Mapping Multi-Rows with ElementCollection [message #1065397 is a reply to message #1064961] Tue, 25 June 2013 14:41 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The Phones table cannot be used in both the parent and child. You need another table for the owner.


James : Wiki : Book : Blog : Twitter
Previous Topic:OPEN - Disable delete an entity when is its linked in relationships
Next Topic:ConversionException When Using Enum in Composite Primary Keys
Goto Forum:
  


Current Time: Thu Apr 25 05:54:04 GMT 2024

Powered by FUDForum. Page generated in 0.03134 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top