0
/**
 * Description of bankbranchcontactdetailsBean
 *
 * @author Vishal Jain 
 */
package com.beans;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Temporal;
import javax.persistence.UniqueConstraint;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import java.io.Serializable;
import java.util.Date;


@Entity(name = "bankbranchcontactdetails")
@Table(name = "bankbranchcontactdetails", schema = "stserptest", uniqueConstraints = { @UniqueConstraint(columnNames = "BankBranchContactId") })
@TableGenerator(name = "bankbranchcontactdetailsgen", table = "bankbranchcontactdetails", pkColumnName = "BankBranchContactId")
public class bankbranchcontactdetailsBean extends SuperBeanClass implements SuperBeanInterface, Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(columnDefinition="mysql->int(10) unsigned", name = "BankBranchContactId", precision = 10, scale = 0, nullable = false, unique = true)
   private Integer Id;

    @Column(columnDefinition="mysql->int(10) unsigned", name = "BankBranchId", precision = 10, scale= 0, nullable = false)
   private Integer _BankBranchId;

    @Column(columnDefinition="mysql->enum('Phone','Mobile','Fax','Telex')", name = "ContactType", length = 6, nullable = false)
   private String _ContactType;

    @Column(columnDefinition="mysql->varchar(20)", name = "Contact", length = 20, nullable = false)
   private String _Contact;


  public bankbranchcontactdetailsBean() {

        _BankBranchId = 0;
        _ContactType = "";
        _Contact = "";
   }


  public bankbranchcontactdetailsBean(Integer __BankBranchId, String __ContactType, String __Contact, Integer __MyCompanyId) {

        _BankBranchId = __BankBranchId;
        _ContactType = __ContactType;
        _Contact = __Contact;
        _MyCompanyId = __MyCompanyId;
   }



    public int getBankBranchContactId() {
        return Id;
    }

    public Integer getBankBranchId() {
        return _BankBranchId;
    }

    public String getContactType() {
        return _ContactType;
    }

    public String getContact() {
        return _Contact;
    }


    public void setBankBranchContactId(int NewValue) {
        Id = NewValue;
    }

    public void setBankBranchId(Integer NewValue) {
        _BankBranchId = NewValue;
    }

    public void setContactType(String NewValue) {
        _ContactType = NewValue;
    }

    public void setContact(String NewValue) {
        _Contact = NewValue;
    }

}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">MimosaTorpedo</property>
        <property name="hibernate.default_schema">stserptest</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>

        <mapping class="com.stserp.beans.bankbranchcontactdetailsBean" />

    </session-factory>
</hibernate-configuration>


/**
 * Description of ManagerClass
 *
 * @author Vishal Jain 
 */
package com.beans;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class ManagerClass {

    private static SessionFactory factory = null;
    private static ManagerClass _ManagerClass = null;

    public static SessionFactory createSessionFactory() {
        SessionFactory sessionFactory;
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        return factory;
    }

    public static ManagerClass getInstance() throws ExceptionInInitializerError {
        try {
            if (factory == null) {
                factory = createSessionFactory();
            }
            if (_ManagerClass == null) {
                _ManagerClass = new ManagerClass();
            }
        } catch (Throwable ex) {
            JOptionPane.showMessageDialog(
                    null,
                    "Failed to create sessionFactory object...\n"
                            + ex.getMessage(), "Error...", 0);
            throw new ExceptionInInitializerError(ex);
        } finally {
            return _ManagerClass;
        }
    }

    public ArrayList<Integer> SaveBeansList(Session session, String entityName,
            ArrayList<? extends SuperBeanClass> BeansList)
            throws HibernateException {
        ArrayList<Integer> IDs = null;
        try {
            IDs = new ArrayList<Integer>();
            for (SuperBeanClass element : BeansList) {
                IDs.add((Integer) session.save(entityName, element));
            }
        } catch (HibernateException e) {
            throw e;
        }
        return IDs;
    }

    public ArrayList<? extends SuperBeanClass> LoadTable(Session session,
            String TableName) throws HibernateException {
        ArrayList<? extends SuperBeanClass> beansList = null;
        try {
            beansList = (ArrayList<? extends SuperBeanClass>) session
                    .createQuery("FROM " + TableName).list();
        } catch (HibernateException e) {
            throw e;
        }
        return beansList;
    }

    public ArrayList<?> LoadConditional(Session session, String _query)
            throws HibernateException {
        ArrayList<?> beansList = null;
        try {
            beansList = (ArrayList<? extends SuperBeanClass>) session
                    .createQuery(_query).list();
        } catch (HibernateException e) {
            throw e;
        }
        return beansList;
    }

    public SuperBeanInterface LoadById(Session session, Class className,
            Integer ID) throws HibernateException {
        SuperBeanInterface BeanInterface = null;
        try {
            BeanInterface = (SuperBeanInterface) session.get(className, ID);
        } catch (HibernateException e) {
            throw e;
        }
        return BeanInterface;
    }

    public void DeleteById(Session session, Class className, Integer ID)
            throws HibernateException {
        try {
            session.delete(session.get(className, ID));
        } catch (HibernateException e) {
            throw e;
        }
    }
}




/**
 * Description of SuperBeanClass
 *
 * @author Vishal Jain 
 */
package com.beans;

public class SuperBeanClass {
    public SuperBeanClass() {
    }
}



/**
 * Description of SuperBeanInterface
 *
 * @author Vishal Jain 
 */
package com.beans;

public interface SuperBeanInterface {
}

I dont know what wrong I am doing, but even though the code gets executed fine, the Table named 'bankbranchcontactdetails' does not get created, here. The database I am using is MySQL. Can anybody help me with that

Vishal Jain
  • 191
  • 2
  • 3
  • 11
  • possible duplicate of [Hibernate: Automatically creating/updating the db tables based on entity classes](http://stackoverflow.com/questions/306806/hibernate-automatically-creating-updating-the-db-tables-based-on-entity-classes) – Aman Gupta Aug 13 '14 at 04:38
  • Thanks for your advice, But Sorry to say that I have already tried, create. With this the new entry gets overwritten every time. I believe and have even tested that 'create' only creates the table, whereas 'update' will create and update the table. I found an article on stack overflow and have tested it earlier. I believe problem lies somewhere else or I am somewhere conceptually wrong. Kindly maintain you support. – Vishal Jain Aug 13 '14 at 05:18
  • I only can suggest for now, that try changing column definition and hibernate configuration. some time if column definition is wrong, it don't create table. also have you looked at generated logs in console while starting application, you may get hint from there too – Aman Gupta Aug 13 '14 at 10:54
  • Trying Changing Column Definitions was a good idea. Thanks. The Problem was with the database specific directions 'mysql->'. I am might be using it wrongly. But for now I tried removing it and it worked. Thanks for your support. One last thing that I need to know is that How do I Mark this Qauestion as Answered?????? Please Help. – Vishal Jain Aug 13 '14 at 12:31
  • You can only accept it after 48 hrs I guess.. just try to improve quality of your answer.. it may help other to understand it better :) good job btw – Aman Gupta Aug 13 '14 at 17:45

1 Answers1

0

Trying Changing Column Definitions here, is a good idea. The Problem was with the database specific directions 'mysql->'. I am might be using it wrongly. But for now I tried removing it and it worked.

Vishal Jain
  • 191
  • 2
  • 3
  • 11