-->
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.  [ 3 posts ] 
Author Message
 Post subject: problems with annotations
PostPosted: Wed Oct 01, 2008 4:20 pm 
Newbie

Joined: Sun Sep 14, 2008 1:35 pm
Posts: 8
Hibernate version: 3

I'm trying to switch domain from xml mapping to annotations but I have a problem. Hibernate throws error:
Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: domain.Category, for columns: [org.hibernate.mapping.Column(parentId)]

old xml file ( working fine):


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="domain.Category" table="categories" lazy="false">
    <id name="id" column="id">
        <generator class="native"/>
    </id>

    <property name="name">
        <column name="name"/>
    </property>
   
    <many-to-one
       name="category"
       cascade="all"
       column="parentId"
       foreign-key="FK_CATEGORIES_1" />
       
    <property name="active">
        <column name="active"/>
    </property>
</class>
</hibernate-mapping>


Domain was normal setter and getter.


New annoted domain class:
Code:
/**
*
*/
package domain;
import javax.persistence.*;

/**
* @author Michał Warecki
*
*/
@Entity
@AttributeOverride( name="category", column = @Column(name="parentId") )
public class Category{
   
   @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
   private Integer id;
   
    private String name;
    private Category category;
    private Integer active;

    public Category() {
    }

   
    public Integer getId(){
        return id;
    }

    public void setId(Integer id){
        this.id = id;
    }
   
    @Column (name="name")
    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
   
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(   name="Category",
               joinColumns = @JoinColumn(name="id"),
               inverseJoinColumns = @JoinColumn(name="parentId")
    )
    public Category getCategory(){
        return category;
    }   

    public void setCategory(Category category){
       this.category = category;
    }
   
    @Column (name="active")
    public Integer getActive(){
        return active;
    }   

    public void setActive(Integer active){
        this.active = active;
    }
}


I'm new with hibernate. Can somebody help me with this error? I can't solved it :-/

Thx for help !


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 3:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

most likely your problem is caused by the fact that you are mixing the placement of the annotations (field vs properties). You have to be consistent in your approach and it is important where you place your @Id. It determines whether the other annotations should also be on field level or on the getters. Try placing the @Id on the getter or put all other annotations on the field level as well.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2008 2:11 pm 
Newbie

Joined: Sun Sep 14, 2008 1:35 pm
Posts: 8
Ok, I've changed my domain but I have another problem.
New version :

Code:
/**
*
*/
package domain;
import javax.persistence.*;

/**
* @author Michał Warecki
*
*/
@Entity
@Table(name="categories")
@AttributeOverride( name="category", column = @Column(name="parentId") )
public class Category {
   
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   private Integer id;
   
   @Column(name="name")
    private String name;
   
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=Category.class,
             optional = true)
    @JoinTable(   name            = "categories",
               joinColumns       = @JoinColumn(name="id"),
               inverseJoinColumns    = @JoinColumn(name="parentId")
    )
    private Category category;
   
    @Column(name="active")
    private Integer active;

    public Category() {
    }

   
    public Integer getId(){
        return id;
    }

    public void setId(Integer id){
        this.id = id;
    }
   
    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
   
   
    public Category getCategory(){
        return category;
    }   

    public void setCategory(Category category){
       this.category = category;
    }
   
    public Integer getActive(){
        return active;
    }   

    public void setActive(Integer active){
        this.active = active;
    }
}


The problem is:

when I'm putting domain to db and I'm setting 'category' attr. on null everything is ok but when I'm putting to 'category' object ( Category ) hibernate throws an error:

Code:
org.springframework.dao.DataIntegrityViolationException: could not insert: [domain.Category]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [domain.Category]
   org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:624)
   org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:744)
   dao.CategoryDao.saveOrUpdate(CategoryDao.java:37)
   web.form.addCategoryFormController.onSubmit(addCategoryFormController.java:30)
   org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:409)
   org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:381)
   org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
   org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
   org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.