请注意,此代码适用于普通的Spring,但不适用于Spring Boot(v1.3.3),是否有一些我缺少的东西,因为这是从一个有效的弹簧应用程序导入的。下面的代码来自spring boot app
@Entity
@Table(name="project")
public class Project implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="teamId")
private int teamId;
//private String Rentabiliteit;
@Column
//@Index(name="IProject_status",columnNames="Status")
private String status;
@Column
//@Index(name="IProject_naam",columnNames="Naam")
private String naam;
//public Prototype m_Prototype;
//public Team m_Team;
}
SQL
CREATE TABLE IF NOT EXISTS `project` (
`id` int(11) NOT NULL,
`teamId` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`naam` varchar(255) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=latin1;
ERROR
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Unknown column 'project0_.team_id' in 'field list'
编辑:Application.yml
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
datasource:
url: jdbc:mysql://localhost:3306/oxyplast
username: oxyplastuser
password: oxyplastuserpw
jpa:
properties:
hibernate:
current_session_context_class: org.springframework.orm.hibernate4.SpringSessionContext
namingStrategy: org.hibernate.cfg.DefaultNamingStrategy
答案 0 :(得分:25)
Spring Boot提供ImprovedNamingStrategy
作为默认命名策略,这使得Hibernate搜索team_id
列(从int teamId
字段推断)。由于您的表中不存在此列,因此这是错误的原因。来自Hibernate文档:
一种改进的命名策略,更喜欢嵌入式下划线到混合大小写的名称
你有两个选择:
明确提供列名称为@Column(name="teamId")
。早期的Boot版本曾经有bug,而不再是。{/ p>
更改Spring Boot属性中的命名策略并告诉它使用EJB3NamingStrategy
,它不会将camelCase转换为snake_case,而是保持原样。
<强>更新强>
从1.4开始,由于切换到Hibernate 5,命名策略已更新为SpringPhysicalNamingStrategy
,should be very close为1.3默认值。
另见:
答案 1 :(得分:2)
如果您使用的是Spring Boot 2.0.2和Hibernate 5.3.4,则设置以下属性将解决此问题。
max
答案 2 :(得分:1)
以下策略对我有用
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
答案 3 :(得分:0)
使用已故版本:
spring-boot-starter-data-jpa: ➡ 1.5.2.RELEASE
hibernate-core:5.0.12.Final
这个班级
PhysicalNamingStrategyStandardImpl
需要扩展并添加到hibernate属性中。
这是一个完整的工作版本
public class PhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable {
public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
String nameModified;
// Do whatever you want with the name modification
return new Identifier(nameModified, name.isQuoted());
}
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
String nameModified;
// Do whatever you want with the name modification
return new Identifier(nameModified, name.isQuoted());
}
在配置数据源时,应该像这样将它链接到hibernate。
properties.put("hibernate.physical_naming_strategy", "my.Package.PhysicalNamingStrategyImpl");
这是datasource config
的完整工作版本import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.xxxxxx.repository" }
)
public class SharedDataSourceConfig {
@Value("${startup.ddl-auto}")
String hbm2ddl;
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties("spring.datasource.shared")
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
properties.put("hibernate.physical_naming_strategy", "my.package.PhysicalNamingStrategyImpl");
return builder
.dataSource(dataSource)
.packages(PackageScannerHelper.getPackagesToScan())
.persistenceUnit("shared")
.properties(properties)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
答案 4 :(得分:0)
这对我来说适用于Spring Boot 1.4.0和HibernateEntitymanager 4.3.8.Final
application.properties
spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
答案 5 :(得分:0)
application.properties
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
以上属性对我有用。 休眠4.3.11.Final 弹簧靴1.4.2.RELEASE
答案 6 :(得分:-1)
如果你想在列名中使用UpperCase,你必须这样使用
@Column(name="\"teamId\"")
private int teamId;
你必须使用EJB3NamingStrategy或ImprovedStrategyNaming强制进行休眠。