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 ( and using ElementCollection to record employee phone numbers:

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

Short version

What I need is a class like this:

  public class PhoneId {
  long owner_id;

  List<Phone> phones;

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

Long version

I follow the tutorial code:

public class PhoneId {
  long owner_id;

  List<Phone> phones = new ArrayList<Phone>();

class Phone {
  String type = "";
  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 =
  EntityManager entityManager = entityFactory.createEntityManager();

  // Create new entity
  Phone ph = new Phone("home", "001-010-0100");
  PhoneId phid = new PhoneId();


but it keeps throwing exceptions

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: Sun Oct 04 03:40:45 GMT 2015

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

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