14

I read https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/. I tried suggestion config like(using spring data JPA,hibernate 5.0 as vendor ):

public class PaperSubjectType{
    @Id
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private PaperSetting paperSetting;
..
}

class PaperSetting{
  @Id
  @GeneratedValue
  private Long id;
..
}

first I tried the example:

PaperSetting paperSettingInDb = paperSettingRepository.findOne(1);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setSubjectCode("91");
paperSubjectType.setPaperSetting(paperSettingInDb);

paperSubjectTypeRepository.save(paperSubjectType);

error:detached entity passed to persist:PaperSetting. it seems hibernate take PaperSetting as detached when cascade

2 if I want to create both PaperSubjectType and PaperSetting together,do I need to do this:

PaperSetting paperSetting = new PaperSetting();
paperSetting.setxx;
PaperSetting  paperSettingInDbNew = paperSettingRepository.save(paperSetting);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setPaperSetting(paperSettingInDbNew);
paperSubjectTypeRepository.save(paperSubjectType);

or I should use bidirectional in this situation? thank you!

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
yuxh
  • 924
  • 1
  • 9
  • 23

3 Answers3

2

I think you may have forgotten to wrap the logic in a @Transactional block

@Transactional
PaperSetting paperSettingInDb = paperSettingRepository.findOne(1);
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setSubjectCode("91");
paperSubjectType.setPaperSetting(paperSettingInDb);

paperSubjectTypeRepository.save(paperSubjectType);

without that crudRepository.findOne() will open it's own short lived transaction so when you get the return of findOne() the entity is already detached, hence the error

Zeromus
  • 4,472
  • 8
  • 32
  • 40
  • I add @ Transactional @ Commit in my test,this time " Field 'id' doesn't have a default value" – yuxh Dec 08 '17 at 03:53
  • 1
    @yuxh your PaperSubjectType don't have autogeneration annotations on the id... so you either set it before save or add the autogeneration – Zeromus Dec 10 '17 at 19:48
1

I tried it Hibernate 5.2 and it works like a charm.

Assuming you have these entities:

@Entity(name = "Person")
public static class Person  {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String registrationNumber;

    public Person() {}

    public Person(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }
}

@Entity(name = "PersonDetails")
public static class PersonDetails  {

    @Id
    private Long id;

    private String nickName;

    @OneToOne
    @MapsId
    private Person person;

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

And this data access logic:

Person _person = doInJPA( this::entityManagerFactory, entityManager -> {
    Person person = new Person( "ABC-123" );
    entityManager.persist( person );

    return person;
} );

doInJPA( this::entityManagerFactory, entityManager -> {
    Person person = entityManager.find( Person.class, _person.getId() );

    PersonDetails personDetails = new PersonDetails();
    personDetails.setNickName( "John Doe" );
    personDetails.setPerson( person );

    entityManager.persist( personDetails );
} );

The test passes just fine in Hibernate ORM.

Maybe it was a bug in 5.0 that got fixed, so you are better of upgrading.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • ,I find the reason: I have to add @ JoinColumn(name = "id") on private PaperSetting paperSetting; or it will create a extra "paper_setting_id" column in PaperSubjectType table when save PaperSetting . Do you know why? – yuxh Dec 08 '17 at 11:35
  • 1
    There's no need for that if you use `@MapsId`. – Vlad Mihalcea Dec 08 '17 at 11:39
  • 2
    ,I set hibernate.hbm2ddl.auto=none and try again. I have to use @ JoinColumn(name = "id") with @ MapsId or it complain can't find column 'paper_setting_id' when insert – yuxh Dec 12 '17 at 08:51
0

1) Add cascading option:

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@MapsId
private PaperSetting paperSetting;

2) Having that in place, you can save only PaperSubjectType while creating both entities anew:

PaperSetting paperSetting = new PaperSetting();
paperSetting.setxx;
PaperSubjectType paperSubjectType = new PaperSubjectType();
paperSubjectType.setPaperSetting(paperSettingInDbNew);
paperSubjectTypeRepository.save(paperSubjectType);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63