0

I do not really want to talk about why one approach is better than other. I started with annotations and could not get it working simple as it was. When tried using XML configuration it worked. But why? How to debug this kind of problem?

I have a pretty simple existing table "tRegion" with 2 String columns - "Code" and "Region" where Code field is the unique key.

I create a class:

@Entity
@Table(name = "tRegion")
public class Region {
    @Id String Code;
    @Column String Region;

    public void setCode(String code) {
        Code = code;
    }
    public void setRegion(String region) {
        Region = region;
    }
    public String getCode() {
        return Code;
    }
    public String getRegion() {
        return Region;
    }
}

If I run the code:

List<Region> result = (List<Region>) session.createQuery("from Region").list(); 

I get the error:

INFO: HHH000397: Using ASTQueryTranslatorFactory org.hibernate.hql.internal.ast.QuerySyntaxException: Region is not mapped [from Region]

Something is wrong with my annotations... However if I add an XML mapping file:

<hibernate-mapping package="com.mypackage">
<class name="Region" table="tRegion">
    <id name="Code" />
    <property name="Region"/>
</class>
</hibernate-mapping>

it works. For me the mapping looks equivalent to my annotations. What could be wrong? I'm using Hibernate 4.3.1 libraries if it could make any difference.

Edited/solution:

Everybody seemed to agree on solution to modify the persistence.xml, however I'm a beginner in Entity management in Java, so I had no clue I was supposed to use one. Well, after reading suggestions, I even tried to add it (I enabled JPA facet and added the class element to persistence.xml), but I guess I had to do something else to make it use JPA (tried to add @PersistanceContext annotation with no success)

I am also not keen to add another configuration file. In fact the way I deploy my application prevents me modifying any configuration files, and having configuration files that you do not use to eventually modify configuration are kind of pointless.

The approach that finally worked was to simply add .addAnnotatedClass(com.mypackage.Region.class) to my routine that instantiate SessionFactory.

    public static SessionFactory createSessionFactory() {
            Configuration configuration = new Configuration();
            configuration
                    .addAnnotatedClass(com.mypackage.Region.class)
                    .configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            return configuration.buildSessionFactory(serviceRegistry);
    }

3 Answers 3

0

Try adding

<class>xx.xx.Region<class>

To your persitence.xml

0

For mapping entities to Hibernate, besides annotating them with @Entity, you have to do one of the following:

Add all entity classes to your persistence.xml manually

    <class>com.mypackage.Region</class>

Or, in my opinion, the best option, put all your entities in a separated jar and add it to persistence.xml

    <jar-file>lib\your-entities.jar</jar-file>

Note that the jar file path is relative to your final packaged/deployed application

0

How are you configuring which entities will be mapped by Hibernate?

I was able to get your code working by adding your Region entity here (using a HibernateConfiguration class):

@Bean
    public AnnotationSessionFactoryBean sessionFactoryBean() {
...
        AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
        bean.setAnnotatedClasses(new Class[]{Region.class});
...
        return bean;
    }

You can also define your entities in your persistence.xml class like this:

<class>com.mypackage.Region</class>

The documentation on the various ways for configuring Hibernate can be found here.

1
  • Thanks for suggestions. I have no persistence.xml and quickly adding one did not help (I guess missing something else :-) Also I'm not using Spring, so cannot use your suggestion directly; however inspired by your code, I checked how to do it in plain Hibernate. I added the code that worked to my post. Feb 22, 2014 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.