Hie all ,
In the below scenario i am trying to embed a value type object into entity class
Userdetails is the entity class
Address is the value object that i want to embed.
whem i run the TestUser.java(main program) i am getting the below error in console.Please help me where am i am going wrong.
Below are codes for all classes of the program.
Error Message
07:29:37,375 INFO Version:37 - Hibernate Commons Annotations 3.2.0.Final
07:29:37,390 INFO Environment:603 - Hibernate 3.6.4.Final
07:29:37,390 INFO Environment:636 - hibernate.properties not found
07:29:37,390 INFO Environment:814 - Bytecode provider name : javassist
07:29:37,406 INFO Environment:695 - using JDK 1.4 java.sql.Timestamp handling
07:29:37,468 INFO Configuration:2156 - configuring from resource: /hibernate.cfg.xml
07:29:37,468 INFO Configuration:2175 - Configuration resource: /hibernate.cfg.xml
07:29:37,531 WARN DTDEntityResolver:73 - recognized obsolete hibernate namespace
http://hibernate.sourceforge.net/. Use namespace
http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
07:29:37,593 INFO Configuration:2297 - Configured SessionFactory: null
07:29:37,718 INFO AnnotationBinder:532 - Binding entity from annotated class: com.hibernate1.UserDetails
07:29:37,828 INFO EntityBinder:530 - Bind entity com.hibernate1.UserDetails on table UserDetails
07:29:37,921 INFO Configuration:1676 - Hibernate Validator not found: ignoring
07:29:37,937 INFO HibernateSearchEventListenerRegister:75 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.hibernate1.UserDetails column: city (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:694)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at com.hibernate2.TestUser.main(TestUser.java:30)
UserDetails.java
package com.hibernate2;
import java.util.*;
import javax.persistence.*;
@Entity
@Table(name="USEER_DETAILS")
public class UserDetails
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ElementCollection
private Set<Address> listofAddresses = new HashSet();
public Set<Address> getListofAddresses() {
return listofAddresses;
}
public void setListofAddresses(Set<Address> listofAddresses) {
this.listofAddresses = listofAddresses;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Address.java
package com.hibernate2;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class Address {
@Column(name="CITY_NAME")
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
TestUser.java
package com.hibernate2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
@SuppressWarnings("deprecation")
public class TestUser {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDetails user1 = new UserDetails();
user1.setId(1);
user1.setName("santhosh");
Address add1 = new Address();
add1.setCity("HYD");
Address add2 = new Address();
add1.setCity("KKLR");
Address add3 = new Address();
add1.setCity("CHN");
user1.getListofAddresses().add(add1);
user1.getListofAddresses().add(add2);
user1.getListofAddresses().add(add3);
SessionFactory sessionfactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
session.save(user1);
session.getTransaction().commit();
session.close();
}
}
hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/workspace</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.hibernate1.UserDetails"/>
<mapping class="com.hibernate1.Address"/>
</session-factory>
</hibernate-configuration>