Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
/**
 * 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
<?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>


Java
/**
 * 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

I have already tried,
HTML
<property name="hibernate.hbm2ddl.auto">create</property>
. 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 believe problem lies somewhere else or I am somewhere conceptually wrong/week/missing something.
Posted
Updated 12-Aug-14 20:17pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900