0

I am using hibernate as ORM tool is my project. It worked fine for me previously and created table properly.My last table had around 200 columns ,after some changes now my table need 150 columns.So I have edited my mapped pojo class .Have removed extra attributes and getters setters from it Hb2ddl works perfectly as I can see drop table, create table syntax on my console.But new Table generated by hibernate consists some old column schema values too , which are not being used and does't exist in the mapped pojo?

For checking purpose when I change pojo name then it generates rigth table with 150 columns.

Have added image for first condition.Values below mir_ids are from old schema.I have not used them im my mapped pojo.

Image1: enter image description here :

And when to check I try a differnt name for pojo.It I get correct output .(Those are 166 rows)

Image2: :

enter image description here

Those extra value's are not present and works perfectly fine.But I don't want to do it.I will need to change table name in all project.

what can be the reason for it ? How to correct it?

Note: I have edited mapped class name and it worked for me.I got a new table created with 151 colums as I needed but hibernate still creates that old table itself.Even there is no mapping of that in hibernate.cfg.xml file.

Code for hibernate.cfg.xml file:

<property name="hibernate.connection.autocommit">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="hibernate.connection.password">abhijeet</property> <property 
        name="hibernate.connection.url">jdbc:mysql://localhost:3306/jdbctest</property> 
        <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.current_session_context_class">thread</property>
     <property name="hibernate.connection.pool_size">1</property>  
<!--    <property name="hibernate.connection.datasource">jdbc/mysql_pool1</property> --> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>  
    <mapping class="com.abhijeet.nabi.pojos.TablePojo" />

Why does hibernate creates an extra table , for which I don't have any mapping.

Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
  • what value do you have for property hbm2ddl.auto? validate | update | create | create-drop – shazin Sep 22 '14 at 09:40
  • I have used 'create'.Works properly as everytime it creates new table.But some ols schema values are not removed. – Abhijeet Panwar Sep 22 '14 at 09:42
  • @AbhijeetPanwar does [this](http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do) helps you – Ankur Singhal Sep 22 '14 at 10:15
  • @ankur-singhal : Sorry ,I did't get you that what you are asking for.It it was about changing mapped pojo's name then yes, changing mapped object name and creating a new table with same name helps me but want to know reason for it. – Abhijeet Panwar Sep 22 '14 at 10:21

1 Answers1

1

I assume by using the create option you can run into mixed up tables when having structural changes - when you changed your table's name it created a new one and it worked out fine. So during development, you should use the option create-drop - that way you are sure, to have a correct table each time.

During integration and of course production, you should use validate and have the schema created separately.


Update:

I think the problem is a mix of Hibernate and JPA configurations.

You didn't specify the version of Java EE you were using, but since it's Java EE, you do have JPA available and as long as you're not using any proprietary API from Hibernate, there is no need in having a hibernate.cfg.xml, but only a persistence.xml (see here).

Also, you don't need to list all the classes you want to map, but use auto-detection, so it uses all annotated entities automatically.

You also may check out the chapter of the Java EE 7 tutorial.

Community
  • 1
  • 1
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96
  • Thanks for your reply.Edited code.Used create-drop instead but I can see a more awkward thing now.When I do desc tablename; It showed 151 cols .As I am expecting.Then I again did desc tablename but it showed 153 ,One more time and count reached to 155. not its 188.I am more confused now. – Abhijeet Panwar Sep 22 '14 at 10:47
  • @AbhijeetPanwar: How do you deploy your application? Is it within an IDE, does it use Maven or are you copying files manually? And are you using additional configurations, like mapping files? Try to completely clear the deployment and start it again. `drop-create` should always go through all your entities and create them as annotated (you can see this in hibernate log if enabled). Besides that, you can always use the `validate` mechanism during development - we're doing that to keep data which is already in the database and only manually call db reset scripts which renew the schema. – Alexander Rühl Sep 22 '14 at 10:59
  • yes.It is within IDE.I am not using maven with that.Create-drop works properly.As just after deployment it shows 151 columns as I am expecting but then It adds old table schema with it.Strange.afer few mintues it adds old schema values also with schema of new table :( – Abhijeet Panwar Sep 22 '14 at 11:02