Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal to call this method from injected, managed EntityManager

 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 entities i.e Book and Publisher ,which |I am persisting in corresponding tables.
I have joined Book with Publisher entity with onetoone relationship like this:

@OneToOne
@JoinColumn(name = "pubId")
private Publisher publisher;

When I am trying to retrieve and display publisher name using this statement :

Book book = em.find(Book.class, 1)

OR

List <Book> pb = em.createQuery("from Book").getResultList().setParameter("pubid", 1).getResultList();

I'm getting this error:

javax.ejb.EJBException: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager

I am using the same entitymanager to persist and view book and publisher entities in other methods and its working fine,
but for this query its giving error , Any Clue?

Thanks.






 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Book to Publisher is OneToOne? Wouldn't it be a ManyToOne mapping. I mean a Publisher that can only publish one Book is well, not a good publisher. ;)

Mark
 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally agree that OnttoOne relationaship b/w Publisher to Book is not a good example.Actually
I am trying to learn entities relationship and I am very confused about sequence of DML operations
b/w entities. For example if we have two entities

1. Order
2. Invoice
and they both have bidirectional onetoone relationship and both have reference to each other i.e

Order entity :

@OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="order",targetEntity=Invoice.class)
private Invoice invoice;

Invoice

@OneToOne(optional=false)
@JoinColumn(name = "ORDER_ID")
private Order order;

As invoice cannot exist withiout Order ,so first we will save record in Order but
what value will be saved in invoice field as Invoice is yet not saved?
I assume when Invoice will be persisted it will update the corresponding order's invoice field
with newly generated invoice number?
Also when Invoice will be persisted how it will get the value for Order id?






 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic