Mysql Spring应用程序启动验证具有错误数据库表的实体
我在mysql服务器中有两个数据库,名为Mysql Spring应用程序启动验证具有错误数据库表的实体,mysql,hibernate,spring-mvc,hibernate-entitymanager,Mysql,Hibernate,Spring Mvc,Hibernate Entitymanager,我在mysql服务器中有两个数据库,名为Database1&Database2 Database1有一个表Database1.dummy\u table,主键Database1.dummy\u table.id为整数 而Database2有一个表Database2.dummy\u table,主键Database2.dummy\u table.id为biginteger 我的spring配置文件如下: <?xml version="1.0" encoding="UTF-8"?> &l
Database1
&Database2
Database1
有一个表Database1.dummy\u table
,主键Database1.dummy\u table.id
为整数
而Database2
有一个表Database2.dummy\u table
,主键Database2.dummy\u table.id
为biginteger
我的spring配置文件如下:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" default-lazy-init="true"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="default"/>
<property name="packagesToScan" value="com.springapp.mvc.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">com.springapp.dialect.Mysql5CustomDialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/Database1" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>
如果我将Database2.dummy_table.id更改为整数,或者删除schemaDatabase2
应用程序工作正常。服务器如何验证数据库2的表?我使用的是spring4.3.10.RELEASE
,hibernate4.3.7.Final
&Mysql 8.0.12
Database1表脚本:
CREATE TABLE `dummy_table` (
`ID` INT NOT NULL AUTO_INCREMENT,
`MeterNo` VARCHAR(45) NOT NULL,
`Status` INT NOT NULL,
PRIMARY KEY (`ID`));
CREATE TABLE `dummy_table` (
`ID` BIGINT NOT NULL AUTO_INCREMENT,
`MeterNo` VARCHAR(45) NOT NULL,
`Status` INT NOT NULL,
PRIMARY KEY (`ID`));
Database2表脚本:
CREATE TABLE `dummy_table` (
`ID` INT NOT NULL AUTO_INCREMENT,
`MeterNo` VARCHAR(45) NOT NULL,
`Status` INT NOT NULL,
PRIMARY KEY (`ID`));
CREATE TABLE `dummy_table` (
`ID` BIGINT NOT NULL AUTO_INCREMENT,
`MeterNo` VARCHAR(45) NOT NULL,
`Status` INT NOT NULL,
PRIMARY KEY (`ID`));
我的应用程序中的实体:
@Entity
@Table(name = "dummy_table")
public class DummyTable implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@Column(name = "MeterNo")
private String meterNo;
@Column(name = "Status")
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMeterNo() {
return meterNo;
}
public void setMeterNo(String meterNo) {
this.meterNo = meterNo;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
实体必须使用正确的架构或目录进行注释: 尝试使用默认目录调整您的配置:
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">com.springapp.dialect.Mysql5CustomDialect</prop>
<prop key="hibernate.default_catalog">Database1</prop>
</props>
</property>
验证
com.springapp.dialogue.mysql5customdialogue
数据库1
或者,您应该定义并使用仅对数据库1有访问权限的mysql用户,而不是使用
root
。这样,Hibernate将不会“看到”Database2,因此不会尝试将Database2.dummy_表映射到实体。将实体更改为:
@Entity
@Table(catalog = "Database1", name = "dummy_table")
public class DummyTable implements Serializable {
如果添加columnDefinition=“bigint(20)这样的columnDefinition,是否有帮助''到另一个表的映射参数?不,我已经尝试过了。这个问题只有在我只使用特定于模式的用户时才能解决。你能显示与这2个
dummy_表
s匹配的实体类吗?请检查更新的question@Benoit.schema失败,错误相同,但catalog有效。但是如果我用db name t注释我的实体如果在生产环境中更改db名称,该怎么办?它不应该在datasource中配置吗?是的。关于特定于用户的解决方案,您是正确的。我已经检查过了。但MSSQL Server/Oracle仍然没有出现此问题。只有mysql会产生此问题。是的,可以在配置中设置默认目录,请参阅我的答案。但是它是错误的只有默认的(而不是唯一的)一个。