我正在学习bootspring.
findByDate(int date);过去工作,直到我int Date进入内部阶级.
现在我可以保存新条目,但是我无法通过日期对它们进行检索
我需要改变什么?
@Transactional
public interface ExpirationDAO extends JpaRepository<ExpirationDTO, Long> {
public ExpirationDTO findByDate(int date);
}
Run Code Online (Sandbox Code Playgroud)
和
@Embeddable
public static class IdKey implements Serializable{
@NotNull
int date;
@ManyToOne
ProductDTO product;
public IdKey(){
}
//setters and getters
}
@EmbeddedId
private IdKey id;
@NotNull
int units;
public ExpirationDTO(){
}
//setters and getters
}
Run Code Online (Sandbox Code Playgroud)
抛出此异常:
org.springframework.data.mapping.PropertyReferenceException: No property date found for type ExpirationDTO!
Run Code Online (Sandbox Code Playgroud) Hibernate实现了一些标准的命名策略:
default
for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - an alias for jpa
jpa
for org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl - the JPA 2.0 compliant naming strategy
legacy-hbm
for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl - compliant with the original Hibernate NamingStrategy
legacy-jpa
for org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl - compliant with the legacy NamingStrategy developed for JPA 1.0, which was unfortunately unclear in many respects regarding implicit naming rules.
component-path
for org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl - mostly follows ImplicitNamingStrategyJpaCompliantImpl rules, except that it uses the full composite paths, as opposed to just the ending property part
Run Code Online (Sandbox Code Playgroud)
但我找不到每个策略的任何例子.你知道我在哪里找到这样的例子吗?
实体看起来像这样:
@Getter
@Setter
@Entity
public class Application {
@Id
private Long id;
@Enumerated(EnumType.STRING)
private ApplicationStatus status;
}
Run Code Online (Sandbox Code Playgroud)
代码以这种方式工作:
public interface ApplicationRepository extends JpaRepository<Application, Long> {
@Query("SELECT app FROM #{#entityName} AS app WHERE app.status LIKE :status")
List<Application> find(@Param("status") ApplicationStatus status);
Run Code Online (Sandbox Code Playgroud)
但是使用nativeQuery的相同代码段 - 不会:
@Query(value = "SELECT app.* FROM application AS app WHERE app.status LIKE :status", nativeQuery = true)
List<Application> findNative(@Param("status") ApplicationStatus status);
}
Run Code Online (Sandbox Code Playgroud)
而且我没有任何异常,只是空列表.
我怎样才能解决这个问题?是否有可能使用enum与nativeQuery?
PS我可以String转入方法而不是ApplicationStatus但也许有另一种选择?
我创建了Spring应用程序.附加了Pom xml.
它具有这样的配置(如下)和一些用于Flyway数据库迁移工具的db/migration/V1__init.sql.
它有hsqldb内存数据库,它是在应用程序启动后创建的.创作后很干净.
我希望Hibernate基于实体类创建一个模式,然后Flyway填充表格.现在,Flyway在创建表之前启动V1__init.sql并抛出异常.如何更改此订单或我可以执行哪些解决方案?
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!-- For using 'LEGACYHTML5' mode in Thymeleaf -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.21</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency> …Run Code Online (Sandbox Code Playgroud) 我当前用于忽略JPA实体的已知属性的注释是:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","created","updated","createdBy","lastUpdatedBy"})
Run Code Online (Sandbox Code Playgroud)
除了忽略这些类属性之外,我还想忽略服务器接收的任何未知属性.我知道通过以下注释忽略未知属性的单独方法:
@JsonIgnoreProperties(ignoreUnknown=true)
Run Code Online (Sandbox Code Playgroud)
但不知道如何将其添加到我上面给出的当前注释中.我尝试了下面的多种方法,但似乎都没有工作,我找不到这个场景的在线示例.
文档上的任何示例或引导也会有所帮助.
我正在使用Spring Roo,使用Spring MVC和JPA来持久化MySQL数据库.我对Spring MVC和Java非常陌生,但是我一直在使用CakePHP和Rails.
User除了密码之外,我还有一个包含个人详细信息的实体.这样的事情(在其他.aj文件中排除了很多Roo生成的功能):
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;
@Column(name = "PASSWORD", length = 32)
private String password;
@Column(name = "FIRST_NAME", length = 25)
private String firstName;
@Column(name = "LAST_NAME", length = 25)
private String lastName;
@Column(name = "ADDRESS", length = 255)
private String address;
// The appropriate getters and setters
...
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的User控制器中有一个编辑动作,我根据Roo自动生成的脚手架创建了以下约定:
@RequestMapping(value="/edit", method = RequestMethod.GET)
public String editForm(Model uiModel) {
String username = (String) …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data JPA开展项目.我在数据库中有一个表作为my_query.
我想创建一个方法,该方法将字符串作为参数,然后在数据库中将其作为查询执行.
方法:
executeMyQuery(queryString)
Run Code Online (Sandbox Code Playgroud)
例如,当我通过
queryString= "SELECT * FROM my_query"
Run Code Online (Sandbox Code Playgroud)
那么它应该在DB级别运行该查询.
存储库类如下.
public interface MyQueryRepository extends JpaRepository<MyQuery, Long>{
public MyQuery findById(long id);
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "?1", nativeQuery = true)
public void executeMyQuery(String query);
}
Run Code Online (Sandbox Code Playgroud)
但是,它没有像我预期的那样工作.它给出以下错误.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''select * from my_query;'' at line 1
Run Code Online (Sandbox Code Playgroud)
还有其他办法,我可以实现这个目标.提前致谢
我做了一个实验......两个Spring数据库的一个共同实体: - JPA - MongoDB
首先,我使用以下库版本:
spring-data-jpa:1.7.0.RELEASE spring-data-mongodb:1.6.0.RELEASE
我有一个实体:
@Entity
@Table(name = "ACCOUNTS")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ACCOUNT_ID")
private Long id;
@Column(name = "ACCOUNT_NUMBER")
private String number;
public Account() {
}
public Account(String number) {
this.number = number;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
Run Code Online (Sandbox Code Playgroud)
JPA Repository具有以下外观: …
搜索了几个小时,但我陷入了PlayAramework与JPA的学习曲线.我正在建立一个可以发布帖子的示例网站.但这些帖子可以具有以下状态:
这些状态存储在单独的表中.显然,国家职位草案尚不可见.
所以我有这些课程:
在我的页面课程中,我有:
@Column(name="POSTS_REF")
@Where(clause="PostPublished")
private List<Posts> userPosts;
Run Code Online (Sandbox Code Playgroud)
但这不起作用!那么,我如何指定where子句,只加载处于已发布状态的帖子而不使用JPQL?
谢谢!
更新时间:2011-10-11
表:具有列的帖子: - id - title - state_ref(对States表的ID的引用) - 内容
表:美国与列: - ID - Statename的
所以我想说的是:
select *
from posts inner join states on posts.state_ref = states.id
where states.statename = 'PostPublished'
Run Code Online (Sandbox Code Playgroud)
更新2011-10-13
这是我目前的修改,在我的页面类中:但它也不起作用.
/** link to the states */
@JoinColumn(name = "STATES_REF")
@OneToOne
@Where(clause = "states.statename = 'PostPublished'")
public MyState state;
Run Code Online (Sandbox Code Playgroud)
更新2012-02-13 Emt的回答毕竟对我有用.
我用自己的程序创建了数据库,它显示为mydatabase.mv.db文件.
但是当我尝试DbVisualizer使用明显相同的参数访问同一个数据库时,它创建了两个文件mydatabase.lock.db,celebrity.h2.db并且没有看到在程序中创建的表.
什么是不兼容?
UPDATE
两种设置如下:


java ×8
hibernate ×6
jpa ×3
spring ×3
spring-data ×2
sql ×2
dbvisualizer ×1
enums ×1
flyway ×1
h2 ×1
jackson ×1
json ×1
mongodb ×1
mysql ×1
persistence ×1
spring-boot ×1
spring-roo ×1