java hibernate即使用<property name =“hibernate.hbm2ddl.auto”> update </property>也不创建表

时间:2014-08-13 04:35:23

标签: java mysql hibernate

/**
 * 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 {
}

我不知道我在做什么错,但即使代码执行得很好,表名为&#39; bankbranchcontactdetails&#39;这里没有创建。我使用的数据库是MySQL。任何人都可以帮助我吗

1 个答案:

答案 0 :(得分:0)

在这里尝试更改列定义是个好主意。问题在于数据库特定方向'mysql-&gt;'。我可能错误地使用它。但是现在我尝试删除它并且它有效。