hibernate
संस्थाओं के बीच एसोसिएशन मैपिंग
खोज…
वनटोमनी एसोसिएशन
OneToMany के संबंध को समझाने के लिए हमें देश और शहर जैसे 2 संस्थाओं की आवश्यकता है। वन कंट्री में मल्टीपल सिटीज होती हैं।
नीचे दिए गए CountryEntity में हम देश के शहरों के सेट को परिभाषित करते हैं।
@Entity
@Table(name = "Country")
public class CountryEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "COUNTRY_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer countryId;
@Column(name = "COUNTRY_NAME", unique = true, nullable = false, length = 100)
private String countryName;
@OneToMany(mappedBy="country", fetch=FetchType.LAZY)
private Set<CityEntity> cities = new HashSet<>();
//Getters and Setters are not shown
}
अब शहर की इकाई।
@Entity
@Table(name = "City")
public class CityEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CITY_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer cityId;
@Column(name = "CITY_NAME", unique = false, nullable = false, length = 100)
private String cityName;
@ManyToOne(optional=false, fetch=FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private CountryEntity country;
//Getters and Setters are not shown
}
XML का उपयोग करते हुए कई एसोसिएशन में से एक
यह एक उदाहरण है कि आप XML का उपयोग करके एक से कई मैपिंग कैसे करेंगे। हम अपने उदाहरण के रूप में लेखक और पुस्तक का उपयोग करेंगे और मान सकते हैं कि एक लेखक ने कई किताबें लिखी होंगी, लेकिन प्रत्येक पुस्तक में केवल एक लेखक होगा।
लेखक वर्ग:
public class Author {
private int id;
private String firstName;
private String lastName;
public Author(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
}
पुस्तक वर्ग:
public class Book {
private int id;
private String isbn;
private String title;
private Author author;
private String publisher;
public Book() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
Author.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Author" table="author">
<meta attribute="class-description">
This class contains the author's information.
</meta>
<id name="id" type="int" column="author_id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
</class>
</hibernate-mapping>
Book.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Book" table="book_title">
<meta attribute="class-description">
This class contains the book information.
</meta>
<id name="id" type="int" column="book_id">
<generator class="native"/>
</id>
<property name="isbn" column="isbn" type="string"/>
<property name="title" column="title" type="string"/>
<many-to-one name="author" class="Author" cascade="all">
<column name="author"></column>
</many-to-one>
<property name="publisher" column="publisher" type="string"/>
</class>
</hibernate-mapping>
किसी एक को कई कनेक्शन के लिए बनाता है बुक क्लास में एक लेखक होता है और xml में <कई-से-एक> टैग होता है। कैस्केड विशेषता आपको यह निर्धारित करने की अनुमति देती है कि बाल इकाई को कैसे बचाया / अपडेट किया जाएगा।
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow