Why does the class implement the Serializable interface?

@Entity public class Husband implements Serializable { @Id private int id; private String name; @OneToOne private Wife wife; } @Entity public class Wife implements Serializable { @Id private int id; private String name; @OneToOne(mappedBy="wife") private Husband husband; } 
  • What is Serializable in the broadest sense?
  • Why does the class implement the Serializable interface?
  • Why only the husband’s member @OnetoOne (mappedBy = "Wife"), but the wife’s member does not have @OnetoOne (mappedBy = "husband")
+6
source share
4 answers
  • Serialization in the broadest sense is how Java provides developers with the ability to store the state of any object in persistent storage.

  • If a developer wants for some reason an instance of his coded class to be stored in the backup storage, then the class must be declared as implementing Serializable.

  • The above code is a one-to-one relationship between husband and wife. Which basically means that each wife is associated with one husband, and each husband is associated with one wife. :-) Also in the above ratio, the husband is the master of the relationship [in Entity-Relationship terms], and that’s why the Wife says that she is displayed / associated with her husband by her husband, and not vice versa. This means that the husband identifies his wife, and not vice versa.

+10
source

1) The Serializable interface is just a marker. This means that objects can be serialized, that is, they can be represented as a bit string and restored from this bit string.

For example, both of your classes are serializable because they can be represented as a bit string (or regular string). On the other hand, a class that is a file descriptor issued by the operating system cannot be serialized: as soon as the program is finished, this descriptor will disappear and there is no way to return it. Reopening a file by file name is not guaranteed, as it could have been deleted / moved / changed resolution in the meantime.

2) Serialization of objects that do not implement the Serializable interface will result in a NotSerializableException .

3) According to the documentation:

mappedBy
This element is indicated only on the back (non-party) side of the association.

+5
source
  • The Serializable interface helps maintain the state of an instance of an object.

  • According to jpa specification:

    "If an object instance should be passed by value as a separate object (for example, via a remote interface), the entity class must implement the Serializable interface" - Saving JSR 220 - see 2.1 Requirements for the entity class

  • According to java ee documentation:

    "Field to which the relation belongs. This element is indicated only on the back (not belonging to the side) side of the association." - Java EE 6 Documentation

+3
source

I just want to answer the third question because it is not fully explained.

Here is your question:

Why only the husband’s member @OnetoOne (mappedBy = "Wife"), but the wife’s member does not have @OnetoOne (mappedBy = "husband")

Firstly, something is wrong here: it should be: @OneToOne(mappedBy="wife") , not Wife . Note that mappedBy must be followed by an attribute Wife Husband class Wife , not a Wife class , is not a Husband class name although the Husband class is the owner. The Hibernate document says:

mappedBy refers to the name of the association property on the owner side.

Secondly, you need to keep in mind that when comparing a database, there are two types of relationships: unidirectional and bidirectional . We use unidirectional relationships when we want one part to process / support this relationship, but on the other hand we are not interested in receiving data from this side. In your example, if we just need a one-way relationship from the Husband class to Wife , we only want to know, having an object of the Husband type, who is his wife (using the getWife() method), but we are not interested in knowing one lady husband.

In this case, we can do the following: (note that in the Husband class we have an attribute of the Wife type, but in the Wife class we do not have an attribute of the Husband type)

Husband.java:

 @Entity public class Husband implements Serializable { ... @OneToOne //note that @JoinColumn is necessary when mapping, //and although in the table you may have columns like //`ID_WIFE` in table of Husband, in Java it not a number //attribute, it an attribute of `Wife` type. @JoinColumn(name="idWife") private Wife wife; } 

Wife.java:

 @Entity public class Wife implements Serializable { @Id private int id; private String name; //We can omit the attribute `husband` of `Husband` type, //because we are not interested in it. Or, we don't need //to map in the owned side, because it unidirectional. } 

But, if we want to have a bi-directional connection, we must match it both on the side and on the side belonging to it, mappedBy necessary before the attribute belongs.

We remain the Husband side intact, and we change Wife.java :

Wife.java:

 @Entity public class Wife implements Serializable { @Id private int id; private String name; @OneToOne(fetch=FetchType.LAZY, mappedBy="wife") private Husband husband; } 

Check this page: https://en.wikibooks.org/wiki/Java_Persistence/OneToOne , this explains all this in an understandable way.

Thirdly, just a small note: the owner side is usually a table class with the FK of another table. Or, we just remember: the owner owns all this, so he also has an FK in his table.

0
source

Source: https://habr.com/ru/post/896582/


All Articles