During the creation of the database by Hibernate, it is adding a unique key constraint for the foreign key id_student
in oe_iv_student_lang
table because we have to implement the Serializable
interface which results in Hibernate not letting us add multiple rows with same parent foreign key in its corresponding child table.
I have attached the snippet for better understanding..
Student Class:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "oe_iv_student")
public class OeIvStudent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_student")
private Integer idStudent;
}
Student Lang Class:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "oe_iv_student_lang")
public class OeIvStudentLang implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_student_lang")
private Integer idStudentLang;
@OneToOne
@JoinColumn(name = "id_student")
private OeIvStudent idStudent;
@Column(name = "ln_code")
private String lnCode;
}
Location Class:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "oe_locations")
public class OeLocations {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_location")
private Integer idLocation;
@OneToOne
@JoinColumn(name = "id_student", referencedColumnName = "id_student")
private OeIvStudentLang idStudent;
@Column(name = "code")
private String code;
}
The reason I use Serializable
interface is that I am mapping an Object of OeIvStudentLang
class in OeLocations
but the column being referenced is of OeIvStudent
class. Having an Object of OeIvStudentLang
makes it easier for me to naivgate throught the tables, i.e.
OeLocations
->OeIvStudentLang
->OeIvStudent
If I do not use Serializable
in the OeIvStudentLang
class, Hibernate throws a Exception
saying that OeIvStudentLang
is not Serializable
when I work with OeLocations
class.If I were to use an Object of OeIvStudent
instead, I would not be able to navigate to the lang class from OeLocations
.
Please have a look and suggest what to be done.
Thanks :)
This hasn't anything to do with implementing Serializable
.
You've defined the relationship between OeIvStudentLang
and OeIvStudent
as @OneToOne
. This means that every OeIvStudentLang
has exactly one OeIvStudent
. As you've even stated, this should be a One-To-Many relationship, thus you need to change your mapping from @OneToOne
to @OneToMany
and it should remove this unique constraint.
For reference:
A one-to-one association is similar to many-to-one association with a difference that the column will be set as unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With