Cannot generate hibernate mappings from schema

I am trying to get IDEA 12 to generate hibernate (4.1) mappings from schema but it doesnt like it. Every time, regardless of what combination of xml/annotations output, single table selection , etc, I use, it always fails with the error: "basic attribute type should not be object". Then, the OK button is always greyed out meaning I have to close the dialog and start again.

My pom.xml is:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Module_01</groupId>
    <artifactId>Module_01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.5.SP1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>


I have tried assigning the mappings to a sessionFactory and not but nothing works. I am using the OSS pagila schema on postgresql 9.2.1.

[UPDATE] After further hacking, it seems that if the tables that I am attempting to have mapped have foreign-key relationships with other tables that have not yet been mapped then it fails. If I choose a table that has no relations with other tables then it works fine. A bug? Does it work for anyone else?


OK - I was being a doofus. I need to expand all the nodes and I would have seen all the red markers. So, once all the red markers were corrected, the mappings were generated BUT it seemed to want to generate mappings for sequences. This resulted in errors when trying to run any query in the hql console. Any help would be appreciated. I havent tried with other databases yet.

14 comments
Comment actions Permalink

Regarding red markers hidden in the tree: created http://youtrack.jetbrains.com/issue/IDEA-97679

Can't reproduce the issue with PostgreSQL sequences: for me they are filtered out, not shown in Import dialog.

Foreign keys processing is by design. One can deselect and not import problematic relationships.

Regards,
Alexander.

0
Comment actions Permalink

Oops,sequences are not shown, but classes are generated. Created: http://youtrack.jetbrains.com/issue/IDEA-97681

Thank you,
Alexander.

0
Comment actions Permalink

OK, so I have got to the stage where schema is being mapped but I have a couple of issues.
1. The seq are being translated at tables which leads to errors. This I can work around by deleting the generated seq mappings.
2. Id attributes are not being generated - instead they are being created as @Basic attributes. I can edit the mapped classes and change the appropriate attribute to @Id and it seems to work. But, none of the mapping classes auto-generated have @Id attributes. For example, here is a table schema that should generate and @Id attribute but generates a @Basic:


CREATE TABLE actor
(
  actor_id serial NOT NULL,
  first_name character varying(45) NOT NULL,
  last_name character varying(45) NOT NULL,
  last_update timestamp without time zone NOT NULL DEFAULT now(),
  CONSTRAINT actor_pkey PRIMARY KEY (actor_id)
)


If you need to test then you can test against the freely available pagila schema available on pgfoundry. This is all running against PostgreSQL 9.2.2 on x86_64 CentOS 6.3.

0
Comment actions Permalink

It seems that those mappings that get generated without @Id are those representing views.This would be ok but it results in the console not able to run hql queries.

0
Comment actions Permalink

OK - so, inheriting tables are are also generated without @Id. E.g.

-- Table: payment_p2007_01

-- DROP TABLE payment_p2007_01;

CREATE TABLE payment_p2007_01
(
-- Inherited from table payment:  payment_id integer NOT NULL DEFAULT nextval('payment_payment_id_seq'::regclass),
-- Inherited from table payment:  customer_id smallint NOT NULL,
-- Inherited from table payment:  staff_id smallint NOT NULL,
-- Inherited from table payment:  rental_id integer NOT NULL,
-- Inherited from table payment:  amount numeric(5,2) NOT NULL,
-- Inherited from table payment:  payment_date timestamp without time zone NOT NULL,
  CONSTRAINT payment_p2007_01_customer_id_fkey FOREIGN KEY (customer_id)
      REFERENCES customer (customer_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT payment_p2007_01_rental_id_fkey FOREIGN KEY (rental_id)
      REFERENCES rental (rental_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT payment_p2007_01_staff_id_fkey FOREIGN KEY (staff_id)
      REFERENCES staff (staff_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT payment_p2007_01_payment_date_check CHECK (payment_date >= '2007-01-01 00:00:00'::timestamp without time zone AND payment_date < '2007-02-01 00:00:00'::timestamp without time zone)
)
INHERITS (payment)
WITH (
  OIDS=FALSE
);
ALTER TABLE payment_p2007_01
  OWNER TO saqib;

-- Index: idx_fk_payment_p2007_01_customer_id

-- DROP INDEX idx_fk_payment_p2007_01_customer_id;

CREATE INDEX idx_fk_payment_p2007_01_customer_id
  ON payment_p2007_01
  USING btree
  (customer_id);

-- Index: idx_fk_payment_p2007_01_staff_id

-- DROP INDEX idx_fk_payment_p2007_01_staff_id;

CREATE INDEX idx_fk_payment_p2007_01_staff_id
  ON payment_p2007_01
  USING btree
  (staff_id);

0
Comment actions Permalink

Should the mapping classes be generated as inheriting from a mapping class representing the parent table?

0
Comment actions Permalink

Should the mapping classes be generated as inheriting from a mapping class representing the parent table?

0
Comment actions Permalink

Hm, I see that Hibernate documentation really proclaims that entities can be generated for views, so I've created: http://youtrack.jetbrains.com/issue/IDEA-97699

Thank you,
Alexander.

0
Comment actions Permalink

I need some time to investigate the semantics of PostgreSQL inheritance. Will answer you after that.

Thank you for the feedback,
Alexander.

0
Comment actions Permalink

I have briefly investigated related docs. From what I see, I'm not sure how to process PostgreSQL inheritance.

First, PostgreSQL allows multiple inheritance, not possible in Java:
http://www.postgresql.org/docs/9.2/interactive/ddl-inherit.html
How do people work this around?

Second, Hibernate does not suppose a strategy based on SQL inheritance (which is natural, since this PostgreSQL feature is not standard):
http://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html_single/#d0e7071
It mostly looks like table-per-class strategy, but I don't know for sure.

So, I cannot _guess_ how people use it. But if you know a way, which is for sure widely used in real environment, it would be very interesting. Feature request to describe it is welcome:
http://youtrack.jetbrains.com/dashboard

Thank you,
Alexander.

0
Comment actions Permalink

Hi Alexander. I think the way it was generating mappings should be fine if the @Id is correctly applied to the id column. Currently it generates it as @Basic.

0
Comment actions Permalink

Can't reproduce this issue. Primary key constraints are not inherited. If I manually define primary key in child table, then corresponding column gets @Id on generation.

Alexander.

0
Comment actions Permalink

Hi Alexander.

You can see in the DDL snippet that because the table is inheriting from another table - it inherits the primary key, then the mappings generation should take account of that, i.e. it should generate an @Id for the inherited primary key, otherwise the generated mappings will not be 'correct'.

I could definitely go in and manually mark a column as a primary key but this seems unnecessary, particularly when the inherited primary key is displayed as a column in the mapping dialog for the inheritiing table.

0
Comment actions Permalink

The problem is that inherited primary key is not a primary key in PostgreSQL. I.e. when IDEA asks PostgreSQL about payment_id column from the sample above (am I right it was the subject?), the database returns it as just a column.

This is what I see from the documentation and small experiments with my test database. Am I wrong?

Thanks,
Alexander.

0

Please sign in to leave a comment.