java - Repeated column in mapping for entry error -
i doing simple hibernate program in use attribute override , embedded object keys , got error:
exception in thread "main" org.hibernate.mappingexception: repeated column in mapping entity: com.hibernate.model.employee column: pincode (should mapped insert="false" update="false")
employee.java
package com.hibernate.model; import javax.persistence.attributeoverride; import javax.persistence.attributeoverrides; import javax.persistence.column; import javax.persistence.embedded; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; @entity @table (name="employee") public class employee { @id @generatedvalue(strategy=generationtype.auto) private int id; private string firstname; private string lastname; private string email; private string phoneno; @embedded @attributeoverrides({ @attributeoverride (name="city" , column=@column(name="office_city")), @attributeoverride (name="state", column=@column(name="office_state")), @attributeoverride (name="country", column=@column(name="office_country")), @attributeoverride (name="pincode", column=@column(name="office_pincode"))}) private address officeaddress; @embedded private address homeaddress; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { lastname = lastname; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getphoneno() { return phoneno; } public void setphoneno(string phoneno) { this.phoneno = phoneno; } public address getofficeaddress() { return officeaddress; } public void setofficeaddress(address officeaddress) { this.officeaddress = officeaddress; } public address gethomeaddress() { return homeaddress; } public void sethomeaddress(address homeaddress) { this.homeaddress = homeaddress; } }
address.java
package com.hibernate.model; import javax.persistence.embeddable; @embeddable public class address { private string city; private string state; private string country; private string pincode; public string getcity() { return city; } public void setcity(string city) { city = city; } public string getstate() { return state; } public void setstate(string state) { state = state; } public string getcountry() { return country; } public void setcountry(string country) { country = country; } public string getpincode() { return pincode; } public void setpincode(string pincode) { this.pincode = pincode; } }
hibernate.jsp
package com.hibernate.test; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; import com.hibernate.model.address; import com.hibernate.model.employee; public class hibernatetest { public static void main(string[] args) { employee emp = new employee(); address addr=new address(); emp.setfirstname("vikas"); emp.setlastname("bhardwaj"); emp.setemail("bhardwajvikas93@gmail.com"); emp.setphoneno("9741178304"); addr.setcity("mehre"); addr.setcountry("india"); addr.setstate("himachal"); addr.setpincode("174305"); emp.sethomeaddress(addr); address addr2 = new address(); addr2.setcity("bangalore"); addr2.setcountry("india"); addr2.setstate("karnatka"); addr2.setpincode("560008"); emp.setofficeaddress(addr2); sessionfactory sf = new configuration().configure().buildsessionfactory(); session ssn = sf.opensession(); ssn.begintransaction(); ssn.save(emp); ssn.gettransaction().commit(); ssn.close(); } }
cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/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/hibernatedb</property> <property name="connection.username">root</property> <property name="connection.password">vikas</property> <!-- jdbc connection pool (use built-in) --> <property name="connection.pool_size">1</property> <!-- sql dialect --> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <!-- disable second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</property> <!-- echo executed sql stdout --> <property name="show_sql">true</property> <!-- drop , re-create database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- names annotated entity class --> <mapping class="com.hibernate.model.employee"/> <mapping class="com.hibernate.model.address"/> </session-factory> </hibernate-configuration>
in program embedable, printing both member variables in 1 table.
the task address class, printing 2 addresses. 1 home address , 1 office address using attributeoverrides gives me error mentioned above.
you need specify valid property name pincode
@attributeoverride (name="pincode", column=@column(name="office_pincode"))
need change to
@attributeoverride (name="pincode", column=@column(name="office_pincode"))
try specify @attributeoverrides
homeaddress
, if not help.
Comments
Post a Comment