Annotation analysis notes such as JPA one-to-one mapping@OneToOne

Keywords: Hibernate Attribute github Database

I've uploaded a two-way, one-to-one instance to GitHub, the entrance project, and downloaded and run if you're interested, here are two running problems.

Question 1: I left a question at the end of my last blog post.One-to-one Association query comment @OneToOne instance details

Problem point: At the end of the article, one-way one-to-one programs execute the following statements when executing save statements:

Hibernate: select person0_.id as id1_2_1_, person0_.name as name2_2_1_, person0_.pet_id as pet_id3_2_1_, pet1_.id as id1_3_0_, pet1_.pet_class as pet_clas2_3_0_, pet1_.pet_name as pet_name3_3_0_ from person person0_ inner join pet pet1_ on person0_.pet_id=pet1_.id where person0_.id=?
Hibernate: insert into pet (pet_class, pet_name) values (?, ?)
Hibernate: insert into person (name, pet_id) values (?, ?)
Hibernate: insert into pet (pet_class, pet_name) values (?, ?)
Hibernate: update person set name=?, pet_id=? where id=?

What I feel strange about here is that an internal connection was made, so none was found.

Look at the two-way, one-to-one execution:

Hibernate: select husband0_.hid as hid1_1_1_, husband0_.hid_card as hid_card2_1_1_, husband0_.hname as hname3_1_1_, husband0_.w_id as w_id4_1_1_, wife1_.wid as wid1_6_0_, wife1_.wid_card as wid_card2_6_0_, wife1_.wname as wname3_6_0_ from husband husband0_ left outer join wife wife1_ on husband0_.w_id=wife1_.wid where husband0_.hid=?
Hibernate: insert into wife (wid_card, wname) values (?, ?)
Hibernate: insert into wife (wid_card, wname) values (?, ?)
Hibernate: update husband set hid_card=?, hname=?, w_id=? where hid=?
Hibernate: update husband set hid_card=?, hname=?, w_id=? where hid=?

In a two-way one-to-one connection, the first sentence executes a left connection, so the data can be found and updated directly.

Question 2: Exception: Using fastJson as a tool, converting data found in the database to fastJson output throws: java.lang.StackOverflowError: null

The exception is a stack overflow, which can be clearly seen because the husband object was previously nested with the wife and occurred during serialization.I searched some websites, mostly nested lists, to find some clues.Solutions:

Method 1: Unserialize the husband attribute in wife, add the attribute transient, and add the serialVersionUID sequence attribute

private static final long serialVersionUID = 8091602122698339709L;

@Id
// id auto-generation
@GeneratedValue
@Column(name = "wid")
private Long wid;
@Column(name = "wname")
private String wname;
@Column(name = "wid_card")
private String widCard;
 @OneToOne(mappedBy = "wife",cascade=Cascad

eType.ALL)
@JsonBackReference
private transient Husband husband;
Method 2: Rewrite the toString method in bytes instead of using the system's own generated method (there are also tricks here).

husband.toString()


@Override
    public String toString() {
        return "{\"hid\":\"" + hid + "\",\"hname\":\"" + hname
                + "\",\"hidCard\":\"" + hidCard + "\",\"wife\":" + wife
                + "} ";
    }
wife.toString()


@Override
    public String toString() {
        return "{\"wid\":\"" + wid + "\",\"wname\":\"" + wname
                + "\",\"widCard\":\"" + widCard + "\"} ";
    }
    
Nineteen original articles published. 6. 264 visits
Private letter follow

Posted by dsjoes on Fri, 10 Jan 2020 18:19:32 -0800