@OneToMany List vs Set difference
A list, if there is no index column specified, will just be handled as a bag by Hibernate (no specific ordering).
One notable difference in the handling of Hibernate is that you can't fetch two different lists in a single query. For example, if you have a Person
entity having a list of contacts and a list of addresses, you won't be able to use a single query to load persons with all their contacts and all their addresses. The solution in this case is to make two queries (which avoids the cartesian product), or to use a Set
instead of a List
for at least one of the collections.
It's often hard to use Sets with Hibernate when you have to define equals
and hashCode
on the entities and don't have an immutable functional key in the entity.
Difference between @OneToMany and @ElementCollection?
I believe @ElementCollection
is mainly for mapping non-entities (embeddable or basic) while @OneToMany
is used to map entities. So which one to use depend on what you want to achieve.
What is the difference between @ManyToOne(optional=false) vs. @Column(nullable=false)
@Column(nullable=false)
is an instruction for generating the schema. The database column generated off the class will be marked not nullable in the actual database.
optional=false
is a runtime instruction. The primary functional thing it does is related to Lazy Loading. You can't lazy load a non-collection mapped entity unless you remember to set optional=false (because Hibernate doesn't know if there should be a proxy there or a null, unless you tell it nulls are impossible, so it can generate a proxy.)
JPA clear collection and add new items
Turns out that the actual solution was using a @JoinColumn annotation instead of the mappedBy="" parameter.
Related Topics
Environment Variable with Maven
How to Convert Double[] to Double[]
Is It Bad Practice to Use Reflection in Unit Testing
Compareto with Primitives -> Integer/Int
Why Is My Jlabel Not Showing Up
How to Do N-Level Nested Loops in Java
Integer VS Int: with Regard to Memory
Arraylist Contains Case Sensitivity
Java.Lang.Classnotfoundexception: Org.Springframework.Boot.Springapplication Maven
Is -Djava.Library.Path=... Equivalent to System.Setproperty("Java.Library.Path", ...)
Add Multiple Items to an Already Initialized Arraylist in Java
How to Copy Hashmap (Not Shallow Copy) in Java
Steps Needed to Use MySQL Database with Play Framework 2.0
How to Pass C Structs Back and Forth to Java Code in Jni
Is There a Way in Java to Convert an Integer to Its Ordinal Name