Questions tagged [one-to-many]

one-to-many is relationship between two different object properties.

One object property is relating with many properties of other object. Or you can say one object relating to many objects.

In sql, a one-to-many relationship, each row in the related to table can be related to many rows in the relating table.

http://en.wikipedia.org/wiki/One-to-many

3715 questions
601
votes
18 answers

Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags

Hibernate throws this exception during SessionFactory creation: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags This is my test case: Parent.java @Entity public Parent { @Id …
blow
  • 12,811
  • 24
  • 75
  • 112
304
votes
6 answers

What is the meaning of the CascadeType.ALL for a @ManyToOne JPA association

I think I misunderstood the meaning of cascading in the context of a @ManyToOne relationship. The case: public class User { @OneToMany(fetch = FetchType.EAGER) protected Set
userAddresses; } public class Address { …
forhas
  • 11,551
  • 21
  • 77
  • 111
258
votes
11 answers

How to express a One-To-Many relationship in Django?

I'm defining my Django models right now and I realized that there wasn't a OneToManyField in the model field types. I'm sure there's a way to do this, so I'm not sure what I'm missing. I essentially have something like this: class…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
216
votes
8 answers

Difference Between One-to-Many, Many-to-One and Many-to-Many?

Ok so this is probably a trivial question but I'm having trouble visualizing and understanding the differences and when to use each. I'm also a little unclear as to how concepts like uni-directional and bi-directional mappings affect the…
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
176
votes
6 answers

What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association?

In the example section of the @OneToMany JPA annotation reference: Example 1-59 @OneToMany - Customer Class With Generics @Entity public class Customer implements Serializable { ... @OneToMany(cascade=ALL, mappedBy="customer") public…
Behrang
  • 46,888
  • 25
  • 118
  • 160
161
votes
15 answers

Difference between one-to-many and many-to-one relationship

What is the real difference between one-to-many and many-to-one relationship? It is only reversed, kind of? I can't find any 'good-and-easy-to-understand' tutorial about this topic other than this one: SQL for Beginners: Part 3 - Database…
Zhaf
  • 1,804
  • 5
  • 19
  • 16
114
votes
2 answers

JPA: How to have one-to-many relation of the same Entity type

There's an Entity Class "A". Class A might have children of the same type "A". Also "A" should hold it's parent if it is a child. Is this possible? If so how should I map the relations in the Entity class? ["A" has an id column.]
sanjayav
  • 4,896
  • 9
  • 32
  • 30
79
votes
15 answers

Hibernate OneToMany java.lang.StackOverflowError

It's my first question here on stack, so please be gentle :D I'm trying to create hibernate OneToMany relationship. When I try to fetch some data from my DB, I'm getting StackOverflowError. But when i remove OneToMany part, everything goes normally.…
otocon
  • 1,029
  • 1
  • 8
  • 15
78
votes
2 answers

JPA - Persisting a One to Many relationship

Maybe this is a stupid question but it's bugging me. I have a bi-directional one to many relationship of Employee to Vehicles. When I persist an Employee in the database for the first time (i.e. it has no assigned ID) I also want its associated…
JMM
  • 3,922
  • 6
  • 39
  • 46
77
votes
2 answers

Doctrine 2 OneToMany Cascade SET NULL

The error Cannot delete or update a parent row: a foreign key constraint fails. The classes class Teacher { /** *@ORM\OneToMany(targetEntity="publication", mappedBy="teacher") */ protected $publications; } class Publication { …
Nicolas Lino
  • 771
  • 1
  • 5
  • 4
76
votes
4 answers

Hibernate/JPA ManyToOne vs OneToMany

I am reading currently the documentation of Hibernate regarding the entity associations and I come accross a little difficulty to figure out some things. It has to do in essence with the difference between ManyToOne and OneToMany associations.…
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
75
votes
1 answer

AttributeError: 'int' object has no attribute '_sa_instance_state'

I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a…
Ganye
  • 2,481
  • 3
  • 16
  • 13
70
votes
3 answers

When to use inverse=false on NHibernate / Hibernate OneToMany relationships?

I have been trying to get to grips with Hibernate's inverse attribute, and it seems to be just one of those things that is conceptually difficult. The gist that I get is that when you have a parent entity (e.g. Parent) that has a collection of…
James Allen
  • 6,406
  • 8
  • 50
  • 83
57
votes
20 answers

Deleted object would be re-saved by cascade (remove deleted object from associations)

I have the following two entities: 1- PlayList: @OneToMany(fetch = FetchType.EAGER, mappedBy = "playlist", orphanRemoval = true, cascade = CascadeType.ALL) @OrderBy("adOrder") private Set PlaylistadMaps = new…
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
55
votes
4 answers

One-To-Many relationship gets duplicate objects without using "distinct". Why?

I have 2 classes in a one-to-many relationship and a HQL query that is a bit strange. Even if I have read some questions already posted, it does not seem clear to me. Class Department{ @OneToMany(fetch=FetchType.EAGER, mappedBy="department") …
bogdan.herti
  • 1,110
  • 2
  • 9
  • 18
1
2 3
99 100