-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: one to may and many to one mapping
PostPosted: Thu Dec 16, 2010 4:04 am 
Newbie

Joined: Thu Dec 16, 2010 3:53 am
Posts: 1
Hi all,

i m new to hibernate and just practicing some example..

but when i create a one to many /many to one directional data in the child table is not inserted and giving error.

SEVERE: Servlet.service() for servlet sprhibernet threw exception
java.sql.SQLException: Field 'INDIA_CITY_ID' doesn't have a default value


here ''INDIA_CITY_ID' is the foreign key in child table.

my code//

<class name="com.City" table="india_cities">
<id name="cityId" column="CITY_ID">
<generator class="native"/>
</id>
<property name="cityName" column="CITY_NAME" type="string"/>
<set name="regions" inverse="true" cascade="all" lazy="false">
<key column="INDIA_CITY_ID" />
<one-to-many class="com.Region"/>
</set>

</class>


<class name="com.Region" table="india_cities_region">
<id name="regionId" column="REGION_ID">
<generator class="native"/>
</id>
<many-to-one name="city" column="CITY_ID" lazy="false" />
<property name="regionName" column="REGION_NAME" type="string"/>
</class>


child table:-

CREATE TABLE `india_cities_region` (
`REGION_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`INDIA_CITY_ID` INT(10) UNSIGNED NOT NULL,
`REGION_NAME` VARCHAR(50) NOT NULL,
`CITY_ID` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`REGION_ID`),
INDEX `FK5758E3A2E581A145` (`INDIA_CITY_ID`),
INDEX `FK5758E3A21D874A3D` (`INDIA_CITY_ID`),
CONSTRAINT `FK_india_cities_region_india_cities` FOREIGN KEY (`INDIA_CITY_ID`) REFERENCES `india_cities` (`CITY_ID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=3

Parent table :-

CREATE TABLE `india_cities` (
`CITY_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`CITY_NAME` VARCHAR(50) NOT NULL,
`Type` ENUM('High','Low') NULL DEFAULT NULL,
`LATITUDE` DECIMAL(45,10) NULL DEFAULT NULL,
`LONGITUDE` DECIMAL(45,10) NULL DEFAULT NULL,
`WIKIPEDIA_LINK` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`CITY_ID`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=12

Beans in parent(City class)

private int cityId;
private String cityName;
private Set<Region> regions = new HashSet<Region>();

////

Child class

private int regionId;
private City city;
private String regionName;


main test class

City city = new City();
city.setCityName(request.getParameter("city"));
Region region = new Region();
region.setRegionName(request.getParameter("region"));
regions.add(region);
city.setRegions(regions);
region.setCity(city);


//rentService.addCity(city); here it is inserted in db using hibernet ..

Thanks
Harsh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.