hibernate4.2.20 id 用varchar+自定义主键后,可以看到主键生成,但是看不到hinernate输出SQL信息,数据也没有插入

还是学习吧 发布于 2015/09/05 19:53
阅读 376
收藏 0

对象如下定义:

package com.kala.message.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "message")
public class Message implements Serializable{

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String message;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

}

hibernate里面映射对象,如果id用自增,则数据可以正常保存。

有问题的写法

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "com.common.IdentifierGeneratorImpl")
private String id;

-------------------------------------------------------

public class IdentifierGeneratorImpl implements IdentifierGenerator {
private static final int IDLENG = 32;
private static final String YYMMDDHHMMSS = "yyyyMMddhhmmss";
@Override
public String generate(SessionImplementor arg0, Object arg1) throws HibernateException {
return new StringBuilder().append(getCurrentDate()).append("-").append(RandomStringUtils.randomNumeric(IDLENG))
.toString();
}
private static String getCurrentDate() {
return new SimpleDateFormat(YYMMDDHHMMSS).format(new Date());
}
}

如果采用自定义的生成策略就保存不了,后台也不报错。

当ID 使用int+auto_increment时,可以看到hinernate输出SQL信息,数据也可以插入
但是换成varchar+自定义主键后,其他设置不变,可以看到主键生成,但是看不到hinernate输出SQL信息,数据也没有插入,但是也不抛异常,我就不知道是哪里出了问题,求解答

下面是我的事务配置



<!-- 配置hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.kala" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
<prop key="hibernate.c3p0.timeout">120</prop>
<prop key="hibernate.jdbc.fetch_size">60</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.c3p0.acquire_increment">3</prop>
<prop key="hibernate.c3p0.validate">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
</props>
</property>
</bean>


<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用注解的包路径 -->
<context:component-scan base-package="com.kala.message**" />
<context:annotation-config />
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven transaction-manager="transactionManager" />

加载中
返回顶部
顶部