Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you really haven't given alot on the definition of your objects, I am going to make some assumptions.</p> <ol> <li>Home to Person is One to Many</li> <li>Person can only belong to one House</li> </ol> <p>Make sure the house is defined like:</p> <pre><code>@Entity public class House implements Serializable { @Id private int id; @OneToMany(mappedBy="house") private Set&lt;Person&gt; people; ... rest of your class } @Entity public class Person implements Serializable { @Id private int id; @ManyToOne(targetEntity=House.class) private House house; @Column(name="person_name") private String name ... rest of your class public Person(House house, String name) { this.house = house; this.name = name; } } </code></pre> <p>now your code:</p> <pre><code>beginTransaction(); House house = houseDao.find(1L); commitTransaction() ... your magic Person person = new Person(house,"Dilbert"); session.saveOrUpdate(person); </code></pre> <p>In the example above, when you are working with Parent/Child relations (doesn't matter it it is one to many or many to many), you can make the relationship through the child. I generally stay away from just doing blanket updates through the parent. In your example you see that is alot of over head. When you start working where there is thousands of records, it becomes impossible. </p> <p>The other thing to look into, depending on your model is making subtle changes to the annotations to your lists. An Example:</p> <pre><code>@Entity public class House implements Serializable { @Id private int id; @OneToMany(mappedBy="house") @Fetch(FetchMode.JOIN) private Set&lt;Person&gt; people; ... rest of your class } </code></pre> <p>Given that @Fetch is not part of the JPA spec, but a hibernate annotation, this, depending on your model can give a big performance boost because it will grap the house object and all the people in a single query. This is very effective if your are limiting the number of houses and people that belongs to house. If you are getting very large resultsets, this might not be a good situation. The next example might be more suitable:</p> <pre><code>@Entity public class House implements Serializable { @Id private int id; @OneToMany(mappedBy="house") @Fetch(FetchMode.SUBSELECT) private Set&lt;Person&gt; people; ... rest of your class } </code></pre> <p>This will use two querys to grab all the objects. One query for the House object (or multiply house objects depending on the query) and One query for all the people for the house object(s). </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload