Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you specify to not create an Entity as a table when auto-create is set?

I have an application that uses Hibernate 4.1 and Spring 3.1.1. I am using Spring's HibernateJpaVendorAdapter and setting generateDdl to true to create the entities.

I just created a View and created an Entity to map to that View. The Entity for the view is annotated with @Entity, @Table(name="ViewName") and @Immutable. When I deploy the web app it automatically creates tables for all Entities which creates a table for the entity that is supposed to map to my View. I have to go in and manually drop that table and then create the view. While I could continue to do this I wanted to know if there was a way to specify to not create a table for that particular "View" Entity.

like image 254
wxkevin Avatar asked Mar 29 '13 15:03

wxkevin


People also ask

Can an entity class work without having a definition for all columns of the table?

Of-course if you use it only for querying purpose then it should be fine.

Why is Hibernate not creating tables?

Hibernate can create table, hibernate sequence and tables used for many-to-many mapping on your behalf but you have to explicitly configure it by calling setProperty("hibernate. hbm2ddl. auto", "create") of Configuration object.

Which property is used for auto creation of tables in Hibernate?

A developer can have Hibernate create tables by adding the hbm2ddl. auto property and setting it to one of the following four values: validate. update.

How will you create a table from JPA entities?

Right-click the JPA project in the Project Explorer and select JPA Tools > Generate Entities from Tables. On the Select Tables page of the Generate Entities from Tables wizard, select your database connection and schema. To create a new database connection, click Add connection.


1 Answers

If you mark it as an entity then hibernate will always try to make the table if the hibernate.hbm2ddl.auto property is set to update, create-drop, or create. To stop this, get rid of this setting in your config file.

As Kevin pointed out these options are supposed to only be used during the developement phase or really bad things could happen to your prod db. Be sure to remove them before deploying there.

Check out the hibernate docs for more information

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/session-configuration.html

like image 147
Jimmy Johnson Avatar answered Dec 05 '22 06:12

Jimmy Johnson