Announcement Announcement Module
Collapse
No announcement yet.
Getting mapping error for @OneToMany Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting mapping error for @OneToMany

    Hi All,
    I am new to Spring-ORM.
    I have two classes: Country and State
    County is having One to many relationship with State
    Below is my classes and configurations:

    Country.java
    Code:
    @Entity
    @Table(name = "COUNTRY")
    public class Country {
    
    	private int id;
    	private String countryName = null;
    	private List<State> state = null;
    
    	public Country(){}
    
    	@Id
    	@Column(name = "COUNTRY_ID")
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	@Column(name = "COUNTRY_NAME")
    	public String getCountryName() {
    		return countryName;
    	}
    
    	public void setCountryName(String countryName) {
    		this.countryName = countryName;
    	}
    	
    	@OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    	@JoinColumn(name="COUNTRY_ID", nullable=false)
    	public List<State> getState() {
    		return state;
    	}
    
    	public void setState(List<State> state) {
    		this.state = state;
    	}
    }
    State.java
    Code:
    public class State {
    
    	private int id;
    	private String stateName = null;
    	private Country country;
    
    	@ManyToOne(targetEntity = Country.class)
    	public Country getCountry() {
    		return country;
    	}
    
    	public void setCountry(Country country) {
    		this.country = country;
    	}
    
    	@Id
    	@Column(name = "STATE_ID")
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	@Column(name = "STATE_NAME")
    	public String getStateName() {
    		return stateName;
    	}
    
    	public void setStateName(String stateName) {
    		this.stateName = stateName;
    	}
    
    }
    Hibernate config file:
    Code:
    	<bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>com.ourbank.employee.dto.Country</value>
    				<value>com.ourbank.employee.dto.State</value>
    				<value>com.ourbank.employee.dto.City</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					<!-- org.hibernate.dialect.OracleDialect -->
    					org.hibernate.dialect.MySQL5Dialect
    				</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    			</props>
    		</property>
    	</bean>
    While I stat the server I get the below error
    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/config/hibernate-config.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.ourbank.employee.dto.Country.state[com.ourbank.employee.dto.State]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    	... 39 more
    Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.ourbank.employee.dto.Country.state[com.ourbank.employee.dto.State]
    	at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:979)
    	at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:530)
    	at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:471)
    	at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
    	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
    	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
    	at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1112)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
    	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    Can anybody help me on this.

    Thanks in advance.

  • #2
    Hello

    1) is better use Set instead of List, since Set cant accept repeated values

    2) Read carefully the documentation about Mapping Association with Hibernate

    you are wrong


    Code:
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    @JoinColumn(name="COUNTRY_ID", nullable=false)
    public List<State> getState() {
    	return state;
    }
    @JoinColumn must be in the other side of the association, because it receives the PK like a FK

    Then enough with (change to Set the collection)

    Code:
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    public List<State> getState() {
    	return state;
    }
    
    
    @ManyToOne
    @JoinColumn(name="COUNTRY_ID") //by default is nullable=false
    public Country getCountry() {
    	return country;
    }
    BTW

    Code:
    public class State {
    Has no defined neither @Entity nor @Table

    Comment


    • #3
      It's working now.
      Thanks a lot for your help.

      Comment

      Working...
      X