下面是我定义的hibernate.cfg.xml
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3307/pankaj</property>
<property name="connection.username">root</property>
<property name="connection.password">hello</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class ="com.Panky.UserDetails" />
<!-- <mapping class ="com.panky.Vehicle" /> -->
</session-factory>
</hibernate-configuration>
下面是测试hibernate的测试类
package com.Panky;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args) {
System.out.println("hello");
UserDetails user=new UserDetails();
user.setId(1);
user.setUsername("Pankaj");
UserDetails user1=new UserDetails();
user1.setId(2);
user1.setUsername("Pankaj");
SessionFactory sessionFactory
= new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.save(user1);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
UserDetails.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name ="User_details")
public class UserDetails {
@Id
@Column(name="ID")
private int id;
@Column(name="User_Name")
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
控制台输出:
Jul 23, 2016 5:08:11 PM org.hibernate.annotations.common.Version
<clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Jul 23, 2016 5:08:11 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.0.Final}
Jul 23, 2016 5:08:11 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 23, 2016 5:08:11 PM org.hibernate.cfg.Environment
buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 23, 2016 5:08:11 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jul 23, 2016 5:08:11 PM org.hibernate.cfg.Configuration
getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jul 23, 2016 5:08:11 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jul 23, 2016 5:08:12 PM
org.hibernate.service.jdbc.connections.
internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for
production use!)
Jul 23, 2016 5:08:12 PM
org.hibernate.service.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
Jul 23, 2016 5:08:12 PM org.hibernate.service.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Jul 23, 2016 5:08:12 PM org.hibernate.service.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver
[com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3307/pankaj]
Jul 23, 2016 5:08:12 PM
org.hibernate.service.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
Jul 23, 2016 5:08:12 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jul 23, 2016 5:08:12 PM org.hibernate.engine.transaction.internal.
TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy
(direct JDBC transactions)
Jul 23, 2016 5:08:12 PM org.hibernate.hql.internal.ast.
ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jul 23, 2016 5:08:12 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists USER_DTLS
Hibernate: create table USER_DTLS
(ID integer not null, address varchar(255),
Joined_Date datetime, User_Name varchar(255), primary key (ID))
Jul 23, 2016 5:08:13 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into USER_DTLS
(address, Joined_Date, User_Name, ID) values (?, ?, ?, ?)
你可以看到它创建了一个表USER_DTLS
当它必须创建user_details时,我删除了用户详细信息中的2个字段,我留下了id&amp; user_name尽管它显示4个字段
USER_DTLS(地址,Joined_Date,User_Name,ID)值(?,?,?,?)
任何人都可以帮助解决hibernate这种奇怪的行为吗?