Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table not created by Hibernate

I annotated a bunch of POJO's so JPA can use them to create tables in Hibernate. It appears that all of the tables are created except one very central table called "Revision". The Revision class has an @Entity(name="RevisionT") annotation so it will be renamed to RevisionT so there is not a conflict with any reserved words in MySQL (the target database).

I delete the entire database, recreate it and basically open and close a JPA session. All the tables seem to get recreated without a problem.

Why would a single table be missing from the created schema? What instrumentation can be used to see what Hibernate is producing and which errors?

Thanks.

UPDATE: I tried to create as a Derby DB and it was successful. However, one of the fields has a a name of "index". I use @org.hibernate.annotations.IndexColumn to specify the name to something other than a reserved word. However, the column is always called "index" when it is created.

Here's a sample of the suspect annotations.


    @ManyToOne
    @JoinColumn(name="MasterTopID")
    @IndexColumn(name="Cx3tHApe")
    protected MasterTop masterTop;

Instead of creating MasterTop.Cx3tHApe as a field, it creates MasterTop.Index. Why is the name ignored?

like image 249
User1 Avatar asked Sep 03 '09 23:09

User1


People also ask

Does Hibernate create tables automatically?

If the table name/field name name is as same as the table name/column name, then you don't have to specify the name attribute. Here, we specify create for the hibernate. hbm2ddl. auto property so Hibernate will create a table from the entity class.

Will JPA create table?

For this reason, the ability to have JPA frameworks -- such as EclipseLink or Hibernate -- create tables and databases as they bootstrap is built right into the specification. These database creation facilities are also a great way to validate a newly set up Hibernate and JPA development environment.

Can we create table without primary key in Hibernate?

Not with Hibernate. It requires a primary key.


5 Answers

In case this helps anybody, this happened to me today and it turned out I was using a reserved word on my entity definition:

@OneToMany(mappedBy="list")
@OrderColumn(name="order")
private List<Wish> wishes;

"order" in this case, and Hibernate just skipped over this class. So check your user defined names! :)

Cheers, Mark

like image 109
markbaldy Avatar answered Sep 28 '22 18:09

markbaldy


Answer to your side question (What instrumentation can be used to see what Hibernate is producing and which errors?)

You can org.hibernate.tool.hbm2ddl.SchemaExport to generate your tables.

AnnotationConfiguration conf = (new AnnotationConfiguration()).configure();
new SchemaExport(conf).create(showHql, run);

The first argument allows you to see which HQL this command generates (CREATE TABLEs etc). The second one is whether it should actually perform any modifications (ie false = dry-run).

So running it with (true, false) will show you exactly what Hibernate would do to your tables, without changing anything.

like image 43
André Chalella Avatar answered Sep 28 '22 16:09

André Chalella


name attribute on @Entity is not what you want to use for this purpose. Use @Table annotation instead:

@Entity
@Table(name="RevisionT")
public class Revision {
like image 22
ChssPly76 Avatar answered Sep 28 '22 18:09

ChssPly76


It is due to column names matches with your underlying Database sql reserved words.....try by changing name of columns.....i had faced same problem by changing names it did work.

like image 39
Aniket Kulkarni Avatar answered Sep 28 '22 16:09

Aniket Kulkarni


For me it was a syntax mistake in columnDefinition. Easily detectable by the debug logs.

like image 35
bentzy Avatar answered Sep 28 '22 16:09

bentzy