gpt4 book ai didi

hibernate - Spring MVC Hibernate + SQLite 不创建数据库

转载 作者:行者123 更新时间:2023-12-03 06:22:39 25 4
gpt4 key购买 nike

我正在使用 Spring MVC、Hibernate 和 SQLite,当我运行服务器时,一切正常,但数据库没有创建,也不异常(exception)。当我从 java 类启动 persistence.xml 时,它会创建数据库

public static void main(String[] args) throws Exception {

ApplicationContext context = new ClassPathXmlApplicationContext(
"/META-INF/persistence-config.xml");
String[] names = context.getBeanDefinitionNames();

System.out.println(context.getBeanDefinitionCount());
for (String name : names) {
System.out.println("name Bean == " + context.getBean(name).getClass());
}

}

这是persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Load database property file -->

<tx:annotation-driven transaction-manager="transactionManager" />

<context:annotation-config />

<!-- Tell Spring where to scan for components -->
<context:component-scan base-package="com.binov" />
<context:component-scan base-package="org.hibernate" />


<!-- Create datasource bean -->

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:franchise.db" />
<property name="username" value="" />
<property name="password" value="" />
</bean>


<!-- Create Entity manager - JPA -->

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.binov.franchise.*" />

<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>

<!-- <property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property> -->

<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

</bean>

<tx:annotation-driven />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven />

</beans>

这是SQLiteDialect

package org.hibernate.dialect;

import java.sql.Types;
import org.hibernate.dialect.Dialect;

import org.hibernate.dialect.function.AbstractAnsiTrimEmulationFunction;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.type.StandardBasicTypes;

public class SQLiteDialect extends Dialect {
public SQLiteDialect() {
registerColumnType(Types.BIT, "boolean");
registerColumnType(Types.TINYINT, "tinyint");
registerColumnType(Types.SMALLINT, "smallint");
registerColumnType(Types.INTEGER, "integer");
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.FLOAT, "float");
registerColumnType(Types.REAL, "real");
registerColumnType(Types.DOUBLE, "double");
registerColumnType(Types.NUMERIC, "numeric($p, $s)");
registerColumnType(Types.DECIMAL, "decimal");
registerColumnType(Types.CHAR, "char");
registerColumnType(Types.VARCHAR, "varchar($l)");
registerColumnType(Types.LONGVARCHAR, "longvarchar");
registerColumnType(Types.DATE, "date");
registerColumnType(Types.TIME, "time");
registerColumnType(Types.TIMESTAMP, "datetime");
registerColumnType(Types.BINARY, "blob");
registerColumnType(Types.VARBINARY, "blob");
registerColumnType(Types.LONGVARBINARY, "blob");
registerColumnType(Types.BLOB, "blob");
registerColumnType(Types.CLOB, "clob");
registerColumnType(Types.BOOLEAN, "boolean");

//registerFunction( "abs", new StandardSQLFunction("abs") );
registerFunction( "concat", new VarArgsSQLFunction(StandardBasicTypes.STRING, "", "||", "") );
//registerFunction( "length", new StandardSQLFunction("length", StandardBasicTypes.LONG) );
//registerFunction( "lower", new StandardSQLFunction("lower") );
registerFunction( "mod", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "?1 % ?2" ) );
registerFunction( "quote", new StandardSQLFunction("quote", StandardBasicTypes.STRING) );
registerFunction( "random", new NoArgSQLFunction("random", StandardBasicTypes.INTEGER) );
registerFunction( "round", new StandardSQLFunction("round") );
registerFunction( "substr", new StandardSQLFunction("substr", StandardBasicTypes.STRING) );
registerFunction( "substring", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substr(?1, ?2, ?3)" ) );
registerFunction( "trim", new AbstractAnsiTrimEmulationFunction() {
protected SQLFunction resolveBothSpaceTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "trim(?1)");
}

protected SQLFunction resolveBothSpaceTrimFromFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "trim(?2)");
}

protected SQLFunction resolveLeadingSpaceTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "ltrim(?1)");
}

protected SQLFunction resolveTrailingSpaceTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "rtrim(?1)");
}

protected SQLFunction resolveBothTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "trim(?1, ?2)");
}

protected SQLFunction resolveLeadingTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "ltrim(?1, ?2)");
}

protected SQLFunction resolveTrailingTrimFunction() {
return new SQLFunctionTemplate(StandardBasicTypes.STRING, "rtrim(?1, ?2)");
}
} );
//registerFunction( "upper", new StandardSQLFunction("upper") );
}

public boolean supportsIdentityColumns() {
return true;
}

/*
public boolean supportsInsertSelectIdentity() {
return true; // As specify in NHibernate dialect
}
*/

public boolean hasDataTypeInIdentityColumn() {
return false; // As specify in NHibernate dialect
}

/*
public String appendIdentitySelectToInsert(String insertString) {
return new StringBuffer(insertString.length()+30). // As specify in NHibernate dialect
append(insertString).
append("; ").append(getIdentitySelectString()).
toString();
}
*/

public String getIdentityColumnString() {
// return "integer primary key autoincrement";
return "integer";
}

public String getIdentitySelectString() {
return "select last_insert_rowid()";
}

public boolean supportsLimit() {
return true;
}

public boolean bindLimitParametersInReverseOrder() {
return true;
}

protected String getLimitString(String query, boolean hasOffset) {
return new StringBuffer(query.length()+20).
append(query).
append(hasOffset ? " limit ? offset ?" : " limit ?").
toString();
}

public boolean supportsTemporaryTables() {
return true;
}

public String getCreateTemporaryTableString() {
return "create temporary table if not exists";
}

public boolean dropTemporaryTableAfterUse() {
return true; // TODO Validate
}

public boolean supportsCurrentTimestampSelection() {
return true;
}

public boolean isCurrentTimestampSelectStringCallable() {
return false;
}

public String getCurrentTimestampSelectString() {
return "select current_timestamp";
}

public boolean supportsUnionAll() {
return true;
}

public boolean hasAlterTable() {
return false; // As specify in NHibernate dialect
}

public boolean dropConstraints() {
return false;
}

/*
public String getAddColumnString() {
return "add column";
}
*/

public String getForUpdateString() {
return "";
}

public boolean supportsOuterJoinForUpdate() {
return false;
}

public String getDropForeignKeyString() {
throw new UnsupportedOperationException("No drop foreign key syntax supported by SQLiteDialect");
}

public String getAddForeignKeyConstraintString(String constraintName,
String[] foreignKey, String referencedTable, String[] primaryKey,
boolean referencesPrimaryKey) {
throw new UnsupportedOperationException("No add foreign key syntax supported by SQLiteDialect");
}

public String getAddPrimaryKeyConstraintString(String constraintName) {
throw new UnsupportedOperationException("No add primary key syntax supported by SQLiteDialect");
}

public boolean supportsIfExistsBeforeTableName() {
return true;
}

public boolean supportsCascadeDelete() {
return true;
}

/* not case insensitive for unicode characters by default (ICU extension needed)
public boolean supportsCaseInsensitiveLike() {
return true;
}
*/

public boolean supportsTupleDistinctCounts() {
return false;
}

public String getSelectGUIDString() {
return "select hex(randomblob(16))";
}
}

最佳答案

您是否尝试过在 JDBC URL 属性中放置绝对 URL(具有 W/R 权限)?

例如:

<property name="url" value="jdbc:sqlite:c:\\franchise.db" /> (in Windows)

检查数据库是否在正确的路径(C:/franchise.db)上创建。

关于hibernate - Spring MVC Hibernate + SQLite 不创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com