aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Mapping " Watch "Hibernate Mapping " New topic
Author

Hibernate Mapping

prakash Ranjan
Greenhorn

Joined: Dec 17, 2010
Posts: 1
sir...i am new for hibernate...i make two files names as Login.hmb.xml and Fees.hbm.xml....and i had created two table in postgresDB names as login and fees.

Login.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" default-lazy="false">

<class
name="dream.dao.hibernate.Login"
table="login"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="increment" />
</id>

<property
name="loginid"
type="java.lang.String"
column="loginid"
not-null="true"
unique="true"
length="20"
/>
<property
name="password"
type="java.lang.String"
column="password"
not-null="true"
length="20"
/>

<property
name="email"
type="java.lang.String"
column="email"
not-null="false"
length="30"
/>
<property
name="address"
type="java.lang.String"
column="address"
not-null="false"
length="60"
/>

<property
name="phno"
type="int"
column="phno"
not-null="false"
length="11"
/>


</class>
</hibernate-mapping>

Login.java

package dream.dao.hibernate;

import java.io.Serializable;

public class Login implements Serializable {

/** identifier field */
private Integer id;

/** persistent field */
private String loginid;

/** persistent field */
private String password;

/** persistent field */
private String email;


/** persistent field */
private String address;


/** persistent field */
private int phno;

/** full constructor */
public Login(Integer id, String loginid, String password, String email, String address, int phno) {
this.id = id;
this.loginid = loginid;
this.password = password;
this.address = address;
this.phno = phno;
this.email = email;
}

/** default constructor */
public Login() {
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLoginid() {
return this.loginid;
}

public void setLoginid(String loginid) {
this.loginid = loginid;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getPhno() {
return phno;
}

public void setPhno(int phno) {
this.phno = phno;
}

}

fees.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" default-lazy="false">

<class
name="dream.dao.hibernate.Fees"
table="fees"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="increment" />
</id>

<property
name="loginid"
type="java.lang.String"
column="loginid"
not-null="true"
unique="true"
length="20"
/>

<property
name="amount"
type="java.lang.Integer"
>
<column name="amount" sql-type="integer" not-null="true" length="11"></column>
</property>


</class>
</hibernate-mapping>

Fees.java
package dream.dao.hibernate;

import java.io.Serializable;

public class Fees implements Serializable {

/** identifier field */
private Integer id;

/** persistent field */
private String loginid;

/** persistent field */
private Integer amount;

/** full constructor */
public Fees(Integer id, String loginid, Integer amount) {
this.id = id;
this.loginid = loginid;
this.amount = amount;
}

/** default constructor */
public Fees() {
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLoginid() {
return this.loginid;
}

public void setLoginid(String loginid) {
this.loginid = loginid;
}

public Integer getAmount() {
return this.amount;
}

public void setAmount(Integer amount) {
this.amount = amount;
}

}

Tabels login and fees


CREATE TABLE "login"
(
id integer NOT NULL,
loginid character varying(20) NOT NULL,
"password" character varying(20) NOT NULL,
email character varying(30),
address character varying(60),
phno integer DEFAULT 0,
CONSTRAINT primary_id PRIMARY KEY (id),
CONSTRAINT unique_loginid UNIQUE (loginid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "login" OWNER TO postgres;


CREATE TABLE fees
(
id integer NOT NULL,
loginid character varying(20) NOT NULL,
amount integer NOT NULL,
CONSTRAINT pk_sal PRIMARY KEY (id),
CONSTRAINT fk_sal FOREIGN KEY (loginid)
REFERENCES "login" (loginid) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
OIDS=FALSE
);
ALTER TABLE fees OWNER TO postgres;


Now my problem is that..when i am giving correct data they everything is fine but when i given wrong data also its inserting data fine without error...ex:--in my fees.hbm.xml and fees table the datatype of 'amount' column is integer.whenever i am giving char type value it also accept it and store it without throwing error..so how to fix this issue ..thanks in advance..please do needhul...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate Mapping