我正在尝试使用Spring的JdbcTemplate来简化我在Tomcat中部署并连接到Postgres的Java Web服务中的DAO.
我正在关注Spring的文档,我正在尝试在应用程序上下文文件中配置DataSource,如下所示:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.manta" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<context:property-placeholder location="/WEB-INF/db.properties"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
我db.properties在适当的地方有以下文件:
driverClassName=org.postgresql.Driver
url=jdbc:postgresql://pgprod.ecnext.com:5432/manta
username=my_user_name
password=my_password
Run Code Online (Sandbox Code Playgroud)
当我尝试部署时,我发现以下堆栈跟踪catalina.out:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException:
Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/context.xml]:
Could not resolve placeholder 'driverClassName'
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:220)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:656) …Run Code Online (Sandbox Code Playgroud) 我有以下服务:
@Service
public class CamelService {
@Transactional
public aCamelThing() {
Camel camel = this.camelRepository.findOne(1);
System.out.println(camel.getCamelName()); // prints "normalCamel"
// simple hql set field 'camelName'
int res = this.camelRepository.updateCamelName(1,"superCamel!");
Camel camelWithNewName = this.camelRepository.findOne(1);
System.out.println(camelWithNewName .getCamelName()); // prints "normalCamel" ?!?!?
}
}
Run Code Online (Sandbox Code Playgroud)
我知道如何实现第二张println打印的目标:"superCamel!" ?(将第二次调用与新事务分开并不理想).
假设您有以下两个实体,Address和Customer,它们之间具有一对一的映射关系.
@Entity
public class Address {
@Id @GeneratedValue
private Long id;
private String street_1;
private String street_2;
private String city;
private String state;
private String zipcode;
private String country;
@OneToOne(mappedBy = "address")
private Customer customer;
//getters, setters, and constructors
}
@Entity
public class Customer {
@Id @GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
@OneToOne @JoinColumn( name = "address_id" )
private Address address;
//getters, setters, and constructors
}
Run Code Online (Sandbox Code Playgroud)
另外,我有这两个存储库类:
public interface AddressRepository …Run Code Online (Sandbox Code Playgroud) 我最近升级到Spring Data MongoDB 1.6.0.RC1,非常好,并实现了MongoDB 2.6(huuurah!)的全文搜索功能.我的问题是:我如何组合Criteria和TextCriteria类来生成复杂的查询?
示例对象模型:
{
textProperty: "a text that is indexed for full text search",
language:"en",
aBooleanProperty: true,
anIntegerProperty: 1
}
Run Code Online (Sandbox Code Playgroud)
查询:
db.collection({ anIntegerProperty: 1, $text: { $search: "indexed", $language: "en" } })
Run Code Online (Sandbox Code Playgroud)
那么,如何使用Spring Data MongoDB类编写上述查询?
有一个投影UserProjection,User表有字段 - enable_email布尔值.
@Projection(name = "summary", types = User.class)
public interface UserSummaryProjection {
String getEmail();
}
Run Code Online (Sandbox Code Playgroud)
访问URL /app/users/{id}?projection=summary按预期显示电子邮件
如何总结投影配置为返回的电子邮件仅enable_email是true?
该配置是否仅影响此预测或适用于User实体的所有预测?
我正在使用Spring Boot并尝试使用Spring Data REST实现实体之间的多对多关系,其中不会Author创建依赖实体的重复项.
给定以下代码,当我发布初始书籍时,与作者一起创建作者,创建书籍,并创建关系(查找表条目).当我使用同一作者发布第二本书时,会创建第二本书,并尝试创建第二位作者,由于对作者姓名的唯一约束而导致第二本书失败.
我是否期望/想要发生,是否创建了第二本书,并且将第二本书与原始作者记录相关联,而不是尝试创建第二本/重复作者.
@Entity(name = 'book')
class Book {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
long id
...
@ManyToMany(fetch = FetchType.EAGER, cascade = [CascadeType.ALL])
@JoinTable(name = 'books_authors', joinColumns = @JoinColumn(name = 'book_id'),
inverseJoinColumns = @JoinColumn(name = 'author_id'))
@RestResource(exported = false)
Set<Author> authors
}
@Entity(name = 'author')
@ToString
class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
long id
@Column(nullable = false, unique = true)
String fullName
@ManyToMany(mappedBy = "authors")
Set<Book> books
}
@RepositoryRestResource(collectionResourceRel = "authors", path = "authors")
interface AuthorRepository extends PagingAndSortingRepository<Author, Long> …Run Code Online (Sandbox Code Playgroud) 嗨我正在使用spring数据休息,并且当我使用PagingAndSortingRepository时,我得到一个奇怪的问题,我得到的响应有一个_self链接,但是它也提供了同一个实体的重复链接hf:foo作为以下响应中的evindent.
dupicate links "self":{"href":" http:// localhost:8080/foos/8445 "}和"hf:foo":{"href":" http:// localhost:8080/foos/8445 "
curl http://localhost:8080/foos?page=0&size=1
{
"_links" : {
"first" : {
"href" : "http://localhost:8080/foos?page=0&size=1"
},
"prev" : {
"href" : "http://localhost:8080/foos?page=0&size=1"
},
"self" : {
"href" : "http://localhost:8080/foos"
},
"next" : {
"href" : "http://localhost:8080/foos?page=2&size=1"
},
"last" : {
"href" : "http://localhost:8080/foos?page=81&size=1"
}
},
"_embedded" : {
"hf:foos" : [ {
"name" : "comsi",
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/8445"
},
"hf:foo" : {
"href" : "http://localhost:8080/foos/8445"
} ] …Run Code Online (Sandbox Code Playgroud) 我在使用 EntityManager Here my User 实体从数据库中获取数据时遇到问题
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@Column(name = "username", length = 20, nullable = false)
@JsonProperty
private String username;
@Column(name = "password", nullable = false, unique = true)
@JsonProperty
private String password;
@Column(name = "enabled", nullable = false)
@JsonProperty
private boolean enabled;
@Column(name = "email", nullable = false, unique = true)
@JsonProperty
private String email;
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<UserRole> userRoles;
//getters and setters
Run Code Online (Sandbox Code Playgroud)
我尝试使用用户名搜索用户:
public …Run Code Online (Sandbox Code Playgroud) 在Spring Data JPA的"入门"示例中,我们创建了一个扩展CrudRepository的接口.但是它只处理一个实体:
import org.springframework.data.repository.CrudRepository;
interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
// methods...
}
Run Code Online (Sandbox Code Playgroud)
在实际应用程序中,有许多实体(表),我们需要为它们执行CRUD操作.使用具有多个(相关或不相关)实体的Spring Data JPA存储库的正确方法是什么?
我是否必须为每个实体创建接口并逐个自动装配它们(这听起来非常疯狂)?
我不是很喜欢Hibernate和Spring Data JPA,我有以下疑问.
我有这个方法签名正确执行查询:
@Repository
public interface AccomodationMediaDAO extends JpaRepository<AccomodationMedia, Long> {
AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId, boolean isMaster);
}
Run Code Online (Sandbox Code Playgroud)
它找到(在AccomodationMedia实体类映射的表上)具有名为idAccomodation的字段的单个记录,该字段设置有由accomodationId方法参数表示的Long值,并且字段isMaster由isMaster布尔值重新呈现.
它工作正常,但我的"问题"是这样做我总是显式传递布尔值isMaster参数的值.
此参数必须始终设置为true,所以我尝试以这种方式更改以前的方法签名:
AccomodationMedia findByIdAccomodationAndIsMaster(Long accomodationId, true);
Run Code Online (Sandbox Code Playgroud)
但IntelliJ显示错误:标识符或预期类型.
为什么?我可以设置必须将此参数的值显式设置为true布尔值吗?
spring ×7
java ×5
jpa ×5
spring-data ×5
rest ×2
datasource ×1
hibernate ×1
jdbc ×1
search ×1
spring-mvc ×1