小编Gui*_* He的帖子

H2数据库:使用jdbcTemplate插入记录时,列"ID"不允许NULL

我使用hibernate的hbm2ddl自动生成模式.这是我的域名:

@Entity
public class Reader {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  Long id;

  @Column(nullable=false,unique=true)
  String name;

  @Enumerated(EnumType.STRING)
  Gender gender;

  int age;

  Date registeredDate = new Date();

// getter and setter ...
}
Run Code Online (Sandbox Code Playgroud)

当我使用hibernate来保存a时reader,它可以正常工作,因为它会产生一个id reader.但是,当我使用jdbcTemplate插入带有纯SQL的记录时,它会报告错误:

org.springframework.dao.DataIntegrityViolationException: StatementCallback; 
SQL [insert into reader(name,gender,age) values('Lily','FEMALE',21)]; 
NULL not allowed for column "ID"; 
    SQL statement:insert into reader(name,gender,age) values('Lily','FEMALE',21) [23502-192]; 
nested exception is org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ID"; 
    SQL statement:  insert into reader(name,gender,age) values('Lily','FEMALE',21) [23502-192]
Run Code Online (Sandbox Code Playgroud)

怎么解决这个?

  1. 我调试发现生成的hb2ddl的DDL是create table Book (id bigint not null, author …

hibernate h2

17
推荐指数
4
解决办法
2万
查看次数

在 Spring JPA Hibernate 中使用蛇形案例

我不得不在我的 Spring Boot 项目中手动配置 hibernate jpa,因为我需要引用 2 个数据源。JPA bean 配置之一如下:

@Configuration
@EntityScan("com.channeljin.common.data.entities.user")
@EnableJpaRepositories(
        entityManagerFactoryRef = "userEntityManagerFactory",
        transactionManagerRef = "userTransactionManager",
        basePackages = "com.channeljin.common.data.repo.user")
public class UserDatasourceConfig {

    @Autowired
    private UserDatasourceProperties properties;

    @Bean
    public DataSource userDatasource() {
        PoolProperties poolProperties = new PoolProperties();
        poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
        poolProperties.setUrl(properties.getUrl());
        poolProperties.setUsername(properties.getUsername());
        poolProperties.setPassword(properties.getPassword());
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setPoolProperties(poolProperties);
        return ds;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        Map<String, String> properties = new HashMap<>();
//        properties.put("hibernate.implicit_naming_strategy",
//                "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl");
//        properties.put("hibernate.physical_naming_strategy",
//                "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        properties.put("hibernate.show_sql", "true"); …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa

7
推荐指数
1
解决办法
3454
查看次数

如何为一个特定页面添加JavaScript?

我在我的项目中使用Zurb Foundation,非常方便.但我很困惑,我怎么能添加自己的JavaScript,这只是在加载该页面时的特定页面的DOM.

我在页面的部分中使用了第三方图表库,我必须使用一些JavaScript初始化图表容器.这些JavaScripts无法组合到final中,app.js因为其他页面不包含图表容器div.那么任何人都可以给我一些建议吗?

编辑:以下是我的项目结构.

src/
??? assets
?   ??? img
?   ?   ??? x.jpg
?   ??? js
?   ?   ??? app.js
?   ?   ??? draw.js
?   ?   ??? xcharts.js
?   ??? scss
?       ??? app.scss
?       ??? news.scss
?       ??? _settings.scss
??? data
??? layouts
?   ??? default.html
??? pages
?   ??? news.html
?   ??? template.html
??? partials
?   ??? news-header.html
??? styleguide
    ??? index.md
    ??? template.html
Run Code Online (Sandbox Code Playgroud)

我尝试在我的图表容器之后添加我的JavaScript代码,但我不能,因为它使用了jQuery.Assuming我的JavaScript是draw.js.gulp将自动合并draw.jsapp.js每个页面.如果我只包括draw.js …

javascript zurb-foundation zurb-foundation-6

3
推荐指数
1
解决办法
2789
查看次数

Spring &amp; Cucumber 集成:@Sql 导入没有执行

我需要在实践中将黄瓜与弹簧结合起来。但是我不能用spring的@Sql注释导入一些测试数据。然而,其他不使用黄瓜的集成测试工作正常。我没找到为什么?黄瓜赛跑者:

@RunWith(Cucumber.class)
@CucumberOptions(features={"classpath:features/"})
public class CucumberIT {

}
Run Code Online (Sandbox Code Playgroud)

和步骤定义:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes={DevDbConfig.class})
@Sql("classpath:test-reader-data.sql")
@Transactional
@ActiveProfiles("dev")
public class StepDefines {

  @Autowired
  ReaderService readerService;

  Reader reader;

  @Given("^a user with name Lily$")
  public void a_user_with_name_Lily() throws Throwable {
    reader = readerService.findByName("Lily"); // reader is null here!
  }

  @Given("^this user Lily exists$")
  public void this_user_Lily_exists() throws Throwable {
      assertThat(reader, notNullValue());
  }

  @Then("^Lily's info should be returned$")
  public void lily_s_info_should_be_returned() throws Throwable {
      assertThat(reader.getName(), is("Lily"));
  }

}
Run Code Online (Sandbox Code Playgroud)

但是下面的测试代码可以正常导入测试数据:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes={DevDbConfig.class})
@Sql("classpath:test-reader-data.sql")
@Transactional
@ActiveProfiles("dev")
public …
Run Code Online (Sandbox Code Playgroud)

testing spring cucumber

3
推荐指数
1
解决办法
1048
查看次数

kotlin的JPA:懒惰和@Transient无法使用hibernate

我在我的项目中使用kotlin和JPA,但懒惰委托的kotlin似乎不适用于hibernate:

    @get:Transient
    private val shop:Shop by lazy { shopService.shop(shopId!!) }
Run Code Online (Sandbox Code Playgroud)

我收到错误:

org.hibernate.MappingException: Could not determine type for: kotlin.Lazy, at table: order, for columns: [org.hibernate.mapping.Column(shop$delegate)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:456) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:423) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.mapping.Property.isValid(Property.java:226) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
... 20 common frames omitted
Run Code Online (Sandbox Code Playgroud)

我在这里失踪了吗?

hibernate jpa spring-data kotlin spring-boot

0
推荐指数
1
解决办法
539
查看次数