Many-to-One Relationship in Hibernate Mappings – Example
This example program demonstrates how to write the many-to-one accociations using the hibernate mapping files.
Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| package hibernate;
import java.io.Serializable;
public class Book implements Serializable{
private long id;
private String title;
private Publisher publisher;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Publisher getPublisher() {
return publisher;
}
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
} |
Publisher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| package hibernate;
import java.io.Serializable;
public class Publisher implements Serializable{
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} |
HibernateExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* source : www.javabeat.net
*/
public class HibernateExample {
public static void main(String args[]){
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Publisher publisher = new Publisher();
publisher.setName("First");
session.save(publisher);
transaction.commit();
transaction = session.beginTransaction();
Book book = new Book();
book.setTitle("Test");
book.setPublisher(publisher);
session.save(book);
transaction.commit();
session.close();
}
} |
Book.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
12
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.Book" table="Book">
<id name="id" column="id" type="java.lang.Long" >
<generator class="increment"/>
</id>
<property name="title" column="title" type="java.lang.String"/>
<many-to-one name="publisher" class="hibernate.Publisher" column="publisher"/>
</class>
</hibernate-mapping> |
Publisher.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.Publisher" table="Publisher">
<id name="id" column="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
</class>
</hibernate-mapping> |
hibernate.cfg.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/SampleDB</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="Publisher.hbm.xml"/>
<mapping resource="Book.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Comments
comments
September 28, 2008
Hibernate