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
28
votes
9 answers

Hibernate @OneToMany Relationship Causes Infinite Loop Or Empty Entries in JSON Result

I have two entities, an entity "movie" and an entity "Clip" each clip belongs to one movie and a movie can have multiple clips. My code looks like: Movie.java @OneToMany(mappedBy = "movie", targetEntity = Clip.class, cascade = CascadeType.ALL,…
Lama
  • 2,886
  • 6
  • 43
  • 59
27
votes
5 answers

JPA Persist parent and child with one to many relationship

I want to persist parent entity with 20 child entities, my code is below Parent Class @OneToMany(mappedBy = "parentId") private Collection childCollection; Child Class @JoinColumn(name = "parent_id", referencedColumnName =…
Gora
  • 417
  • 2
  • 5
  • 14
25
votes
2 answers

Hibernate and H2 "Referential integrity constraint violation" for OneToMany bidirectional mapping

I have two simple beans--FatKid and Hamburgers. I need to be able to not only look up all of the hamburgers someone ate, but also who ate which particular hamburger. FatKid.java import java.util.List; import javax.persistence.CascadeType; import…
Kevin
  • 1,420
  • 2
  • 13
  • 11
25
votes
3 answers

@OrderColumn annotation in Hibernate 3.5

I'm trying to use the @OrderColumn annotation with Hibernate 3.5 @OneToMany(mappedBy = "parent",fetch=FetchType.EAGER, cascade=CascadeType.ALL) @OrderColumn(name = "pos") private List childrenCollection; When retrieving data, everything…
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
25
votes
2 answers

JPA - @OneToMany as a Map

This seems like a common enough case, but as JPA newbie, I am having trouble figuring this out. I'm using EclipseLink and PostgreSQL, but this should relate to just the JPA spec. I have one table PRIMARY that has an ID and then a bunch of other…
dnc253
  • 39,967
  • 41
  • 141
  • 157
24
votes
1 answer

Removing child from collection using JPA

I'm using JPA over Hibernate in my web-app. Here are two entities (only getters are shown): class Child { private Parent parent; @ManyToOne(optional=false) @JoinColumn(name="parent_id", referencedColumnName="parent_id", nullable=false,…
Andrey
  • 3,667
  • 6
  • 27
  • 36
24
votes
2 answers

SQLAlchemy: Delete object directly from one-to-many relationship without using session.delete()

I have the following SQLAlchemy setup: Base = declarative_base() class Post(Base): __tablename__ = 'post' id = Column(Integer, primary_key=True) title = Column(String(30)) comments = relationship('Comment', cascade='all') class…
Varicus
  • 1,523
  • 2
  • 12
  • 12
23
votes
5 answers

Hibernate inserts duplicates into a @OneToMany collection

I have a question concerning Hibernate 3.6.7 and JPA 2.0. Consider following entities (some getters and setters are omitted for brevity): @Entity public class Parent { @Id @GeneratedValue private int id; …
user1014562
  • 233
  • 1
  • 2
  • 5
21
votes
5 answers

OneToMany relationship is not working

My Tables: Product: id, name Offer: id, value, product_id Entities: @Entity @Table(name="product") public class Product implements Serializable { @OneToMany(mappedBy="product") private Set offers; …
Emanuel
  • 6,622
  • 20
  • 58
  • 78
21
votes
6 answers

How to save parent and child in one shot (JPA & Hibernate)

I start showing you my scenario. This is my parent object: @Entity @Table(name="cart") public class Cart implements Serializable{ @GeneratedValue(strategy=GenerationType.IDENTITY) @Id @Column(name="id") private Integer id; …
MDP
  • 4,177
  • 21
  • 63
  • 119
21
votes
2 answers

Hibernate Many-To-One Relationship without Primary Key or Join Table

Problem I would like to start by saying that I realize the database structure is atrocious, but I cannot change it at this point in time. That being said, I have the need to create a one-to-many, bi-directional relationship in Hibernate (4.2.1)…
chrismborland
  • 570
  • 1
  • 3
  • 12
20
votes
4 answers

Constraint violation in Hibernate unidirectional OneToMany mapping with JoinTable and OrderColumn when removing elements

I have a problem when removing elements from a list mapped as described above. Here is the mapping: @Entity @Table( name = "foo") class Foo { private List bars; @OneToMany @OrderColumn( name = "order_index" ) @JoinTable( name =…
tbk
  • 1,516
  • 3
  • 14
  • 21
20
votes
1 answer

Golang Gorm one-to-many with has-one

I'm trying to learn Go and Gorm by building a little prototype order management app. The database is MySQL. With simple queries Gorm has been stellar. However, when trying to obtain a result set involving a combination one-to-many with a has-one…
Michael Babcock
  • 703
  • 2
  • 7
  • 13
20
votes
3 answers

How does hibernate save one-to-many / many-to-one annotations? (Children not saving)

I've inherited a hibernate application and I've run into issues. It seems that the code does not save the child in a One-To-Many relationship. It's bidirectional, but on save of parent object, it doesn't seem to save the child. The Question class…
user1732480
19
votes
4 answers

How to select parent row only if has at least one child?

I have a simple one-to-many relationship. I would like to select rows from the parent only when they have at least one child. So, if there are no children, then the parent row is not returned in the result set. Eg. Parent: +--+---------+ |id| …
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83