my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes uses a non-entity as target entity in relationship attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "uses a non-entity as target entity in relationship attribute" Watch "uses a non-entity as target entity in relationship attribute" New topic
Author

uses a non-entity as target entity in relationship attribute

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
This is the error message I get when I try to run my sample app.

For a one to many bidirectional mapping with many to one on the child. I have both the classes in the same package and listed in persistence.xml.
I am stuck. Any help would be appreciated.

Thanks,
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Ensure the target object is annotated with @Entity, is in the same jar file as the persistence.xml, is listed with the correct package in the persistence.xml, and that you have correctly deployed your application.

If still having issues include the fullexception stack trace and source of the class and persistence.xml.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Now I have a problem with a one to many bidirectional mapping. I get a java.sql.SQLIntegrigyConstraintViolationException Cannot insert NULL into

example of what I have

@JoinColumn(name = "AuthorNo", referencedColumnName = "AuthorNo")
@ManyToOne
private Authors authorNo;


@OneToMany(mappedBy = "authorNo", cascade=CascadeType.ALL)
private List<Books> booksCollection= new ArrayList<Books>();

I would really appreciate help in fixing this problem
Bogdan Baraila
Ranch Hand

Joined: May 23, 2011
Posts: 43
Sonia please post you entity classes entirely and also the part of code where you make the insertion. It will be easier for us and for you to see exactly instead of making assumption of what your problem is.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Phones.java




Customer.java

TestOneToOne.java


persistence.xml




[/code]
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

Try making Customer your driving entity with the following change..

Phones.java

and
Customer.java


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "TELEPHONENUMBERMANDATORY_PHONES_ID": invalid identifier

joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

out of curiosity..does this work
try {
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
No. I am still getting an error

... 2 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "TELEPHONENUMBERMANDATORY_PHONES_ID": invalid identifier

joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

oops .. i hope when you tried em.persist(p); you used your original code
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
yes
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

Had a closer look at your code.
The Customer class has
@Column(name = "PHONES_ID", nullable = false)
private BigDecimal phonesId;

and your Phones class has

@Column(name = "CUSTOMER_ID")
private BigDecimal customerId;

1> Even in bidirectional relationships (an ORM concept) you dont need to have foreign keys in both tables.
2> You dont specify foreign keys in this fashion, you do it using JoinColumn


joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

One to One
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I took it out after I posted the code.

See:

http://forums.oracle.com/forums/thread.jspa?messageID=9645937�
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

From the link you mentioned ...

CUSTOMER.java
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ID", insertable = false, updatable = false)
private Phones telephoneNumberMandatory;

Phones.java
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "telephoneNumberMandatory")
private Customer customer;

The Customer table has the column Phone_ID
The Phone table shouldn't and doesn't not have the column Customer_Id (as its not needed)

So you the way these two tables need to be joined is by the Phone_Id (pk in Phones table and fk in Customer table)

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
IT is a bidirectional oneToOne. That is not where my error is though.

Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "TELEPHONENUMBER_PHONES_ID": invalid identifier

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)


I changed the TelephoneNumberMandatory to TELEPHONENUMBER
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

Yes it is bidirectional OnetoOne, but the tables in database will have just one foreign key.

CUSTOMER.java
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ID", insertable = false, updatable = false)
private Phones telephoneNumberMandatory

The above indicates that the Customer and Phones needs to be joined by CUSTOMER_ID, in that case the PHONE_ID column in Customer is redundant.
Please try with
@JoinColumn(name="PHONE_ID", insertable = false, updatable = false)

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
@OneToOne(optional = false,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "PHONE_ID", unique = true, nullable = false, updatable = false)
private Phones telephoneNumber;


is what I have now, I am getting the following error:


eption [EclipseLink-4002] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "TELEPHONENUMBER_PHONES_ID": invalid identifier

Error Code: 904
Call: INSERT INTO PHONES (TELEPHONENUMBER_PHONES_ID, PHONE_NUMBER) VALUES (?, ?)
bind => [101, 5716120000]
Query: InsertObjectQuery(testonetoone.Phones@1ea0105)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)


I really appreciate. need to get it resolved ASAP
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
fixed it. onetoone works! hurrah
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
http://forums.oracle.com/forums/thread.jspa?threadID=2235014

The onetoone mapping stopped working
himanshu rai
Greenhorn

Joined: Jun 22, 2011
Posts: 12
im having a similar problem with my foreign keys.this is a sample appi have been trying using this particular link

http://netbeans.org/kb/67/java/gui-db-custom.html



but after making changes in the customers and orders class if i try to run the program i get the following error

[class customerrecords.Customers] uses a non-entity [class customerrecords.Countries] as target entity in the relationship attribute [private customerrecords.Countries customerrecords.Customers.countryId].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uses a non-entity as target entity in relationship attribute