Do persistent class properties forming composite key also need to be serializable or transient in case of non-serializable?

StackOverflow https://stackoverflow.com/questions/19044862

Question

I have a persistent class :

public class NotifEventGroupRel implements Serializable{

    private static final long serialVersionUID = 7616645672995663305L;
    private Long eventGroupId;
    private String eventName;

    private NotifEventGroup notifEventGroup; //// composite id property
    private EmailTmplt emailTmplt;   // composite id property
    private SmsTmplt smsTmplt;       // composite id property
 ....
}

Its hbm.xml file is :

<hibernate-mapping>
    <class name="NotifEventGroupRel" table="TBLMEVENTGROUPREL">
    <composite-id>
                <key-many-to-one name="notifEventGroup" class="NotifEventGroup" lazy="false" >
                    <column name="EVENTGROUPID" precision="20" scale="0" not-null="true" />
                </key-many-to-one>
                <key-property name="eventName" type="java.lang.String">
                    <column name="EVENT" />
                </key-property>
    </composite-id>
<many-to-one name="emailTmplt" class="EmailTmplt" lazy="false" not-null="false" >
            <column name="EMAILTMPLTID"></column>
        </many-to-one>
        <many-to-one name="smsTmplt" class="SmsTmplt" lazy="false" not-null="false">
            <column name="SMSTMPLTID" />
        </many-to-one>

    </class>
</hibernate-mapping>

The class of properties forming composite id is :

public class NotifEventGroup {

        private Long eventGroupId;
        private String name;
.......
}


public class EmailTmplt {
    private Long emailTmpltId;
    private String name;
.....   
}

public class SmsTmplt {
    private Long smsTmpltId;
    private String name;
..........
}
  • Do i need to make following properties transient :

(shown below)

private transient NotifEventGroup notifEventGroup; //// composite id property
private transient EmailTmplt emailTmplt;   // composite id property
private transient SmsTmplt smsTmplt;       // composite id property

because findbug gives me Non-transient non-serializable instance field in serializable class error for the fields (if non transient).

  • OR do i have to make their corresponding class serializable ?

And what is the impact of doing any of the above two cases ?

Was it helpful?

Solution

The error is reported because all fields in class implementing Serializable must also be Serializable.

Making fields transient

Making fields transient means that they should be ignored in serialization, and I think you don't want that (because they are included in hibernate-mapping file).

From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:

Variables may be marked transient to indicate that they are not part of the persistent state of an object.

Making classes implement Serializable

The classes NotifEventGroup, EmailTmplt and SmsTmplt should also implement Serializable-interface. Implementing Serializable actually does nothing, it is just a marker interface for notifying users that the class can be serialized.

OTHER TIPS

It's strange that these 3 classes are not serializable now. After all, they are entities, too, so why haven't you made them serializable? If you make them transient, Hibernate will not save them – I doubt you'd want such behaviour, esp. in key fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top