• Post Reply Bookmark Topic Watch Topic
  • New Topic

Emulate @OneToMany association without adding a new Persistent Class

 
Carlos Conti
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

is there anyway through annotations to create a list of arbitrary size to be bound to a persistent class, in such a way that it's not necessary to create an additional class?
Let me pose a short example:

Class A holds a list of "references" to Class B at running time.

Class B has no idea about A, and doesn't need to.

But somewhere I need to recall which references of B are bound to an instance of A.

Normally I would do this creating an intermediary persistent class, let's call it AB, where I would store a reference to both A and B, and would make AB persistent aswell. However if possible since I have to recall those values only at one very specific point of my application, I prefer to avoid creating the intermediary class and let's say store the ids Integer references of B in a persistent member of A, which at running time would be converted in a List with references to instances of class B.

Is that possible?

Manny thanks,

Carlos.
 
Carlos Conti
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

in the meantime until someone figures out a better way, what I do is to virtually load/unload/edit a separate @Transient field of type ArrayList, where I update the references upon update of the string member that holds the list of Integer ids, separated by semicolons.

Regards,

Carlos.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post a code sample? It should be trivial to get the list of B from A if you are using JPA/Hibernate without using a relationship class or Transient. Maybe I am misunderstanding the problem. Posting a code sample would help
 
Arunsanjay Jayarathanam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I haven't misunderstood your problem, I think you are looking for a one to many unidirectional relationship without using an intermediate class as well without an intermediate table in the database?? Assuming that is right,here is an example that provides you a solution

JPA Wiki
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!