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
|