现在我有一个非常奇怪的问题,我无法理解。休眠不会插入数据库
我有休眠正确配置。我可以从我的MySQL数据库加载数据没有任何问题。但我无法插入数据。这是我的hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookmaker</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="javax.persistence.validation.mode">none</property>
<mapping class="de.wettprofi.objects.Bookmaker"/>
<mapping class="de.wettprofi.objects.Match"/>
<mapping class="de.wettprofi.objects.LogEntry"/>
</session-factory>
</hibernate-configuration>
这是关于类LogEntry。下面是一些代码,应该persit对象到数据库:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
BufferedReader reader = request.getReader();
String json = reader.readLine();
try {
LogEntry log_entry = null;
if (json != null && !json.isEmpty()) {
log_entry = new LogEntry();
JSONParser parser = new JSONParser();
JSONObject json_dict = (JSONObject) parser.parse(json);
//initalize proper LogEntry object
}
if(log_entry != null) {
Session session = get_hibernate_session();
System.out.println("Logging of LogEntry into the database\n" + log_entry.toString());
session.save(log_entry);
session.flush();
System.out.println("Logging DONE");
}
} catch(Exception e) {
e.printStackTrace();
}
}
public Session get_hibernate_session()
{
//Opening a hibernate session
SessionFactory sessionFactory = null;
try {
Configuration hibConfiguration = new Configuration().addResource("hibernate.cfg.xml").configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(hibConfiguration.getProperties()).buildServiceRegistry();
sessionFactory = hibConfiguration.buildSessionFactory(serviceRegistry);
} catch(Exception e) {
e.printStackTrace();
}
return sessionFactory.withOptions().openSession();
}
这是LogEntry类对象是什么样子(省略getter和setter):
@Entity
@Table(name = "LogEntries")
public class LogEntry {
public static int MESSAGE = 1;
public static int BOOKMAKER = 2;
public static int CLICKLOG = 3;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "lid")
public int lid;
@Column(name = "id")
public String id;
@Column(name = "date")
public Date date;
@Column(name = "type")
public int type;
@Column(name = "message")
public String message;
@Column(name = "bookmaker")
public String bookmaker;
@Column(name = "match_id")
public int match_id;
@Column(name = "result")
public String result;
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(" lid = " + lid + "\n");
sb.append(" id = " + id + "\n");
sb.append(" date = " + date.toString() + "\n");
sb.append(" type = " + type + "\n");
sb.append(" message = " + message + "\n");
sb.append("bookmaker = " + bookmaker + "\n");
sb.append(" match_id = " + match_id + "\n");
sb.append(" result = " + result + "\n");
return sb.toString();
}
所以,当我现在运行我的Tomcat实例,并且对什么确实发生了,我得到以下输出一看:
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Apr 24, 2013 9:55:18 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Apr 24, 2013 9:55:18 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Logging of LogEntry into the database
lid = 0
id = 50569803719166097161
date = Mon Apr 22 21:01:53 CEST 2013
type = 1
message = MainVC
bookmaker = null
match_id = 0
result = null
Hibernate: insert into LogEntries (bookmaker, date, id, match_id, message, result, type) values (?, ?, ?, ?, ?, ?, ?)
Logging DONE
后然而,这在看看到我的数据库显示,什么也没有写入表。
这是我的表模式:
mysql> describe LogEntries;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| lid | int(11) | NO | PRI | NULL | auto_increment |
| bookmaker | varchar(255) | YES | | NULL | |
| date | datetime | YES | | NULL | |
| id | varchar(255) | YES | | NULL | |
| match_id | int(11) | YES | | NULL | |
| message | varchar(255) | YES | | NULL | |
| result | varchar(255) | YES | | NULL | |
| type | int(11) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
有没有人有一个想法是什么可能会导致这样的麻烦。我无言以对:(
只是指出'get_hibernate_session()'不遵循命名功能的Java标准 – KNU 2014-03-31 12:22:18