我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置:
界面:
public interface Something {
// method definitions
}
Run Code Online (Sandbox Code Playgroud)
实施:
@Component
public class SomethingImpl implements Something {
// implementation
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个运行的JUnit测试SpringJUnit4ClassRunner,我想用它测试我的SomethingImpl.
当我做
@Autowired
private Something _something;
Run Code Online (Sandbox Code Playgroud)
它有效,但是
@Autowired
private SomethingImpl _something;
Run Code Online (Sandbox Code Playgroud)
导致测试失败抛出一条NoSuchBeanDefinitionException消息No qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
但是在测试用例中,我想明确地注入my,SomethingImpl因为它是我想要测试的类.我怎么做到这一点?
我有一个非常标准的MVC设置,包括用于DAO层的Spring Data JPA存储库,一个处理Transactional关注和实现业务逻辑的Service层,以及一个具有一些可爱的基于REST的JSON端点的视图层.
我的问题是将Java 8批量采用Stream到这个可爱的架构中:如果我的所有DAO都返回Streams,我的服务返回那些相同的Streams(但是做Transactional工作),我的视图处理并处理这些Stream,然后通过当我的视图开始处理我的Streams中的Model对象时,Service层创建的事务将被关闭.如果底层数据存储尚未实现我的所有模型对象(它Stream毕竟是尽可能的懒惰),那么我的视图将在尝试访问事务之外的新结果时出错.以前这不是问题,因为我会将结果完全实现到List中 - 但现在我们处于Streams 的勇敢新世界.
那么,处理这个问题的最佳方法是什么?完全将服务层内的结果实现为List并将其交还?让View层将Service层交给一个完成块,以便在事务内部进行进一步的处理吗?
谢谢您的帮助!
我正在使用 spring 框架和工作存储库级别的实现。
我有一堂课:
@Repository
public interface MyClassReadRepository extends ReadRepository<MyClass, Integer>
在这个类中有一个看起来像这样的方法:
@Query("SELECT a FROM MyClass a WHERE a.type IN :myType ORDER BY :someProperty :someOrder")
Page<MyClass> findByTypeAndOrder(@Param("myType") List<MyType> myType, @Param("someProperty")String someProperty, @Param("someOrder")String someOrder, Pageable pageable)
但显然查询结构是错误的:“:someProperty”应该是一个标识符......
我的问题是:如何在上面的示例中传递 order 和 sort 参数?
提前致谢!
我目前在我的application.properties文件中做的是:
spring.datasource.url=jdbc:postgresql://localhost:5432/myDB?currentSchema=mySchema
Run Code Online (Sandbox Code Playgroud)
难道没有其他财产吗?由于它看起来很 hacky 并且根据帖子(仍然找不到链接抱歉,稍后会更新),因此它仅适用于 PostgreSQL 9.4。
我试图@Formula使用下面的一段简单代码来了解注释是如何工作的。
我能够打印出描述和 bidAmount 列的值,但用 @Formula 注释的字段(即,shortDescription 和 AverageBidAmount)返回 null。
谁能帮忙指出这里的代码有什么问题吗?
我在 Mac OSX 上使用Hibernate 5.0、postgresql-9.3-1102-jdbc41 和 TestNG 。
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import org.testng.annotations.Test;
import com.my.hibernate.env.TransactionManagerTest;
public class DerivedPropertyDemo extends TransactionManagerTest {
@Test
public void storeLoadMessage() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
try {
{
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = emf.createEntityManager();
DerivedProperty derivedProperty1 = new DerivedProperty();
derivedProperty1.description = "Description …Run Code Online (Sandbox Code Playgroud) 我有一个正在使用 Spring Boot 构建的 API。API 将返回配方。
我有一个@Entity,它包含我的食谱的所有属性,包括类别、成分和制作食谱所需的步骤等内容。
我有两个 API 端点,一个返回配方列表 (/recipes),另一个返回单个配方的详细信息 (/recipes/49)。
我的问题是,在列出食谱 (/recipes) 的端点中,我不想返回诸如成分和步骤之类的属性,但我确实想在提供有关单个食谱的详细信息的端点中返回它们 (/recipes/49 )。问题是使用 @JsonIgnore 总是从所有 JSON 响应中删除属性,这不是我需要的。
我试图做一些研究,但似乎无法找到解决方案,我能想到的唯一方法是始终循环遍历每个配方并填充某种仅具有必要属性的响应对象他们正在返回的端点,但这“感觉”不对。
有人能够确认您将如何执行此操作吗?
使用mysemma的querydsl显示自动生成的sql查询的任何最佳方式,以便可以轻松查看这些sql查询,并且在使用querydsl时调试sql查询变得容易。
例如: from(qCustomer).where(qCustomer.custId.eq("1"));
我需要知道幕后生成的 sql 并希望记录日志以便我可以轻松调试我的应用程序。
我正在使用 Spring Framework 和 Hibernate 编写一个简单的 Spring MVC REST Web 服务。
\n\n我想序列化以下简单的用户实体:
\n\n伪代码:
\n\n@Entity\npublic class UserEntity implements Serializable\n{\n @Id\n @GeneratedValue(\n strategy = GenerationType.IDENTITY)\n @JsonView(Views.BaseView.class)\n @JsonProperty(value = "id")\n @Column(name = "id_user")\n private Integer id;\n\n @JsonView(Views.UserView.class)\n @JsonProperty(value = "login")\n @Column(name = "login")\n private String login;\n\n @JsonView(Views.UserView.class)\n @JsonProperty(value = "password")\n @Column(name = "password")\n private String password;\n\n @JsonView(Views.UserView.class)\n @JsonProperty(value = "creator")\n @ManyToOne(fetch = FetchType.LAZY)\n @JoinColumn(name = "id_creator", \n referencedColumnName = "id_user")\n private UserEntity creator;\n\n //Accessors\n}\nRun Code Online (Sandbox Code Playgroud)\n\n查看描述文件:
\n\npublic class Views\n{\n …Run Code Online (Sandbox Code Playgroud) 最近,我开始使用Spring Boot进行Web应用程序开发.
这是我的.properties文件内容:
#data source configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/sampledb
spring.datasource.schema=sample
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.minimumIdle=3
spring.datasource.maximumPoolSize=5
#jpa properties configuration
#spring.jpa.show-sql=false
spring.jpa.databasePlatform=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=validate
#spring.jpa.properties.hibernate.default_schema=sample
Run Code Online (Sandbox Code Playgroud)
我实体类的这一部分:
@Entity
@Table(name = "sample_info")
public class SampleInfo implements Serializable{
private Long id;
private String code;
private Long serialNumber;
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sample_info_seq_gen"
)
@SequenceGenerator(
name = "sample_info_seq_gen",
sequenceName = "sample_info_seq",
allocationSize = 1
)
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
} …Run Code Online (Sandbox Code Playgroud) postgresql hibernate spring-data spring-data-jpa spring-boot
我正在按如下方式使用Spring存储库,以便按日期范围进行过滤
public interface CustomerRepo extends CrudRepository<Customer, Long> {
public List<Customer> findByCreatedBetween(LocalDate start, LocalDate end);
}
Run Code Online (Sandbox Code Playgroud)
它非常简单,而且运行良好,但是现在我需要扩展我的休息服务,以考虑其他过滤条件,例如,排序或不排序,按城市和国家/地区进行过滤。调用服务时,可能会设置一些参数,而没有设置其他参数。当然,我无法创建诸如findByCreatedBetween之类的方法来考虑所有可能的数据组合。处理这种情况的最佳方法是什么?
谢谢 !
hibernate ×7
spring ×6
java ×4
spring-data ×4
spring-mvc ×4
spring-boot ×3
postgresql ×2
jackson ×1
java-8 ×1
java-stream ×1
jpql ×1
json ×1
mysema ×1
mysql ×1
querydsl ×1