我有Hibernate和Spring框架的Maven项目.我希望Hibernate自动创建表,但是只删除所有现有表并且不创建所需的表.会话工厂初始化期间不会抛出任何异常,但是当我尝试保存Player
实体时,会抛出异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'billboarddb.player'不存在
如果我手动创建表并将属性更改hibernate.hbm2ddl.auto
为"validate"
,则一切正常.你有什么想法,为什么Hibernate不创建表?
Spring配置文件:
<context:component-scan base-package="org.meluk.billboard.business.controller" />
<tx:annotation-driven transaction-manager="txManager" />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<list>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
<property name="packagesToScan" value="org.meluk.billboard.jpa" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop> …
Run Code Online (Sandbox Code Playgroud) 我使用Java EE 5.我为所有EJB编写了一个拦截器,它有三种记录方法:
public class DefaultInterceptor {
public static final String PREFIX = "!!!!!!!!!Interceptor:";
@PostConstruct
public void postConstruct(InvocationContext ctx) {
try {
System.out.println(PREFIX + " postConstruct");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@PreDestroy
public void preDestroy(InvocationContext ctx) {
try {
System.out.println(PREFIX + " predestroy");
System.out.println(PREFIX + "ctx.preceed=" + ctx.proceed());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
System.out.println(PREFIX + "method invocation '" + ctx.getMethod().getName() + "'"); …
Run Code Online (Sandbox Code Playgroud) 我有Spring和Spring安全的Web项目.我的web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" >
<display-name>BillBoard
</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>billboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:security-config.xml classpath:billboard-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>billboard</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
在服务器日志中,我看到Spring上下文被加载了两次(spring bean初始化,数据库创建......).DispatcherServlet首次执行此操作,并在secont时间执行ContextLoaderListener.我该如何解决?
在本教程中,我看到如果出现contextParam,则不需要servlet init-params.但是,如果我删除init params,我有错误:"org.apache.catalina.LifecycleException:org.apache.catalina.LifecycleException:java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/billboard-servlet.xml] ".Dispather servlet在默认位置查找上下文配置.
迭代元素列表是很常见的.检查一些条件并从列表中删除一些元素.
for (ChildClass childItem : parent.getChildList()) {
if (childItem.isRemoveCandidat()) {
parent.getChildList().remove(childItem);
}
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下抛出java.util.ConcurrentModificationException.
在这种情况下,最好的程序模式是什么?
java ×5
spring ×2
annotations ×1
ejb-3.0 ×1
hibernate ×1
interceptor ×1
iteration ×1
optimization ×1
security ×1