google Analytics

Thursday, December 16, 2010

Hibernate one-one Mappng Example




Hibernate – One-to-one relationship example
Hibernate one to one Mapping Example
hibernate one to one annotation example
=========================================================================

The Scripts
CREATE TABLE `user` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1$$

CREATE TABLE `address` (
  `ID` int(11) NOT NULL,
  `ADDRESS` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `FK_ID` (`ID`),
  CONSTRAINT `FK_ID` FOREIGN KEY (`ID`) REFERENCES `user` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

=================================================================================
The User.java Domain Class
@Entity
@Table(name = "user", catalog = "onetwoone")
public class User implements java.io.Serializable {
    private Integer id;
    private String name;
    private Address addresses;
    public User() {
    }
    @Id
    @GeneratedValue(strategy = AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name = "NAME", length = 45)
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
    @Cascade( { CascadeType.ALL })
    public Address getAddresses() {
        return this.addresses;
    }

    public void setAddresses(Address addresses) {
        this.addresses = addresses;
    }
}

===================================================================================================
The Addres class
 To map the association of user in the Address class as a shared primary key association, you also need the @PrimaryKeyJoinColumn annotation:
@Entity
@Table(name = "address", catalog = "onetwoone")
public class Address implements java.io.Serializable {
    private Integer id;
    private User user;
    private String address;
    public Address() {
    }

    // Property accessors
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public User getUser() {
        return this.user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    @Column(name = "ADDRESS", length = 45)
    public String getAddress() {
        return this.address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}
==================================================================================================
Test Programme
public class TestOneTwoOneMapping {
    public static void main(String[] args) {
        User user=new User();
        user.setName("Anish");
        Address address =new Address();
        address.setAddress("USA");
        address.setUser(user);
        user.setAddresses(address);
        new UserDAO().save(user);
    }
}

================================================================================================

The hibernatecfg.xml file
<?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>
    <property name="connection.username">root</property>
    <property name="connection.url">
        jdbc:mysql://localhost/onetwoone
    </property>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="myeclipse.connection.profile">
        com.mysql.jdbc.Driver
    </property>
    <property name="connection.password">password</property>
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="show_sql">true</property>
    <mapping class="session.User" />
    <mapping class="session.Address" />
</session-factory>
</hibernate-configuration>
===============================================================================================
ANish

No comments:

Post a Comment