-->
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.  [ 5 posts ] 
Author Message
 Post subject: Could not determine type for:... with @ManyToOne
PostPosted: Sun Apr 18, 2010 10:53 am 
Newbie

Joined: Wed Mar 11, 2009 7:15 am
Posts: 13
I have problem with @ManyToOne which generate that exception:
Code:
Caused by: org.hibernate.MappingException: Could not determine type for: pl.diagno.model.vo.City, for columns: [org.hibernate.mapping.Column(city)]

In Person is reference to City. When I remove this reference, it works.
I put in a lot of time reading many threads and finding solution but the most similar case is here:
http://forum.springsource.org/showthread.php?t=86745
But there the problem was in the lack of some setters.
Code:
package pl.diagno.model.vo;
import java.util.Date;
import javax.persistence.*;

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
   private long id;
   private String name;
   private String surname;
   private City city;

   public Person(){}
   
   public Person(String name, String surname, City city){   
      this.name=name;
      this.surname=surname;
      this.city=city;
   }
   
   @Column
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }

   @Column
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }

   @Column
   public String getSurname() {
      return surname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }

   @Column
   @ManyToOne(cascade = CascadeType.ALL, targetEntity = City.class)
   @JoinColumn(name="city_id")
   public City getCity() {
      return city;
   }
   public void setCity(City city) {
      this.city = city;
   }
}


Code:
package pl.diagno.model.vo;
import javax.persistence.*;

@Entity
public class City {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
   private long id;
   private String name;
   
   public City(){}
   
   public City(String name){
      this.name=name;
   }
   @Column
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   
   @Column
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      
         <property name="annotatedClasses">
            <list>
               <value>pl.diagno.model.vo.City</value>
               <value>pl.diagno.model.vo.Person</value>
            </list>
         </property>

      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
            <prop key="hibernate.connection.url">jdbc:oracle:thin:@xxx-90ac33161e0:1521:XE</prop>
            <prop key="hibernate.connection.username">diagno</prop>
            <prop key="hibernate.connection.password">1234</prop>
            <prop key="hibernate.default_schema">diagno</prop>
            <prop key="hibernate.connection.pool_size">10</prop>
            <prop key="show_sql">true</prop>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
         </props>
      </property>
   </bean>

   <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@xxx-90ac33161e0:1521:XE" />
      <property name="username" value="diagno" />
      <property name="password" value="1234" />
   </bean>

</beans>

Code:
public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "applicationContext.xml" });
}

Help appreciated!


Top
 Profile  
 
 Post subject: Re: Could not determine type for:... with @ManyToOne
PostPosted: Mon Apr 19, 2010 2:06 am 
Newbie

Joined: Mon Apr 19, 2010 1:44 am
Posts: 2
The annotations must be either on the variables or on the getters. Don't mix and match.
to fix your problem, modify the Person class by doing the following:

- move the @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) from the variable id to the getId() method
- remove the @Column from the getCity() because it is not a column in the Person table (its a foreign key link)

the final class will look like this:

@Entity
public class Person {

private long id;
private String name;
private String surname;
private City city;

public Person(){}

public Person(String name, String surname, City city){
this.name=name;
this.surname=surname;
this.city=city;
}

@Column
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Column
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}


@ManyToOne(cascade = CascadeType.ALL, targetEntity = City.class)
@JoinColumn(name="city_id")
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}


Top
 Profile  
 
 Post subject: Re: Could not determine type for:... with @ManyToOne
PostPosted: Mon Apr 19, 2010 7:08 am 
Newbie

Joined: Wed Mar 11, 2009 7:15 am
Posts: 13
You are my master! ;]
Thank you!
This problem is solved, but another appears. I will describe it in another thread.


Top
 Profile  
 
 Post subject: Re: Could not determine type for:... with @ManyToOne
PostPosted: Thu Apr 22, 2010 12:17 am 
Newbie

Joined: Wed Apr 21, 2010 11:26 pm
Posts: 4
I have a similar problem. In my example, Hibernate could not determine type for CodeDesignator. In my snippet below I've left the setters out. By the way, using Core 3.3.2GA and Annotations 3.4.0GA

Code:
@Entity
public class CodeDesignator {
  private String code;
  private String desc;

  @Id
  @Column(name="CD_CODE")
  public  String getCode() { return code; }

  @Column(name="CD_DESC")
  public String getDesc() { return desc; }
}


Assignment has a many-to-one relationship with CodeDesignator, but does not need to be bi-directional.

Code:
@Entity
@IdClass(Assignment.AssignmentPk.class) // not sure of syntax, typing from memory
public class Assignment {
  private String assignCode;
  private CodeDesignator codeDesignator;
  private String desc;

  @Id
  @Column(name="ASSIGN_CODE")
  public String getAssignCode() { return assignCode; }

  @Id
  @ManyToOne
  @JoinColumn(name="CD_CODE")
  public CodeDesignator getCodeDesignator() { return codeDesignator; }

  @Column(name="ASSIGN_DESC")
  public String getDesc() { return desc; }

  public static class AssignmentPk {
    ...
  }
}


The Assignment table has a compound primary key consisting of ASSIGN_CODE and CD_CODE. If I remove the @Id annotation from getCodeDesignator, Hibernate does not complain. If I leave it in, I get the MappingException where it cannot determine the type for CodeDesignator. I was thinking of leaving out the @Id from getCodeDesignator() and creating a separate code field.

Code:
  private String designatorCode;

  @Id
  @Column(name="CD_CODE")
  public String getDesignatorCode() { return designatorCode; }


Will that work such that when I load an Assignment entity, I can call getCodeDesignator() and get the CodeDesignator entity?


Top
 Profile  
 
 Post subject: Re: Could not determine type for:... with @ManyToOne
PostPosted: Thu Apr 22, 2010 4:41 pm 
Newbie

Joined: Wed Apr 21, 2010 11:26 pm
Posts: 4
rlelina wrote:
The Assignment table has a compound primary key consisting of ASSIGN_CODE and CD_CODE. If I remove the @Id annotation from getCodeDesignator, Hibernate does not complain. If I leave it in, I get the MappingException where it cannot determine the type for CodeDesignator. I was thinking of leaving out the @Id from getCodeDesignator() and creating a separate code field.

Code:
  private String designatorCode;

  @Id
  @Column(name="CD_CODE")
  public String getDesignatorCode() { return designatorCode; }


Will that work such that when I load an Assignment entity, I can call getCodeDesignator() and get the CodeDesignator entity?



Adding the separate code field and removing the @Id from getCodeDesignator() worked. I consider a workaround and not a real solution. I think with JPA2, putting the @Id with getCodeDesignator() as I originally had works.


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

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.