我使用 mysql 数据库编写了一个 Spring启动,hibernate orm 应用程序.... 但是现在我想切换到mongodb ..我使用 Hibernate-ogm 来满足我的要求。当运行应用程序时,控制台显示使用hibernate-ogm成功切换到mongodb ...但是查询无法正常工作...我将我的存储库下面的包导入到hibernate-orm,这些导入与mysql一起工作正常......但是hibernate-ogm @query注释不起作用......
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Query;
这是我的模特
@Entity
@Indexed
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String nic;
private String emp;
private String firstName;
private String lastName;
@NotEmpty
private String mobile;
private String email;
private String status;
private String agency;
@NotEmpty
@Column(unique = true)
private String userName;
@NotEmpty
private String password;
private String userIDandTime;
private String recentUpdateBy;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_roles",
joinColumns = {
@JoinColumn(name = "id")},
inverseJoinColumns = {
@JoinColumn(name = "roleId")})
private Set<Role> userRoles;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_units",
joinColumns = {
@JoinColumn(name = "id")},
inverseJoinColumns = {
@JoinColumn(name = "unitId")})
private Set<Unit> userUnits;
}
这是我的完整存储库
package lk.tradeportal.repository;
import lk.tradeportal.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
/**
* Created by Ignotus on 1/28/2017.
*/
public interface UserRepository extends CrudRepository<User, Long> {
@Override
User save(User user);
@Override
User findOne(Long id);
@Query("select u from User u where u.userName!='super@allSLSI'")
List<User> findAll();
@Query("select u from User u where u.agency = :agency and u.firstName !='admin'")
List<User> findAllStaffUsersByOGA(@Param("agency") String agency);
@Query("select u from User u where u.nic='N/A' and u.firstName='N/A' and u.email='N/A'")
List<User> profileNotCompletedUsers();
@Query("select u from User u where u.agency!='SLSI'")
List<User> chafindAll();
@Query("select u from User u where u.userName = :userName")
User getUserByLoginUserName(@Param("userName") String userName);
@Query("select u from User u where u.id = :userId")
User getUserByUserId(@Param("userId") Long userId);
@Query("select u.email from User u where u.agency !='SLSI'")
List<User> getEmails();
@Query("select a from User a where a.nic = :nic")
User getAgentByNIC(@Param("nic") String nic);
@Override
void delete(Long id);
@Override
void delete(User user);
}
这是我的服务类
package lk.tradeportal.services;
import java.text.SimpleDateFormat;
import java.util.Date;
import lk.tradeportal.domain.Role;
import lk.tradeportal.domain.User;
import lk.tradeportal.repository.RoleRepository;
import lk.tradeportal.repository.UserRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import lk.tradeportal.domain.Unit;
import lk.tradeportal.repository.UnitRepository;
import lk.tradeportal.security.domain.AuthenticatedUser;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* Created by ignotus on 1/31/2017.
*/
@Service
public class UserServices {
private static final Logger serviceLogger = LogManager.getLogger(UserServices.class);
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private UnitRepository unitRepository;
@Autowired
private UserServices UserService;
public User getUserFromUserId(Long id) {
return userRepository.getUserByUserId(id);
}
public List<User> getAllUsers() {
Runtime.getRuntime().gc();
return userRepository.findAll();
}
public User getUserByLoginUserName(String userName) {
return userRepository.getUserByLoginUserName(userName);
}
public List<User> findAllStaffUsersByOGA(String agency) {
return userRepository.findAllStaffUsersByOGA(agency);
}
public List<User> profileNotCompletedUsers() {
return userRepository.profileNotCompletedUsers();
}
public List<User> getChaAllUsers() {
System.out.println(userRepository.chafindAll());
return userRepository.chafindAll();
}
public List<Role> getAllRoles() {
return roleRepository.findAll();
}
public List<Role> findTraderRoles() {
return roleRepository.findTraderRoles();
}
public List<Role> findMOIC() {
return roleRepository.findMOIC();
}
public List<Role> SuperADMINRoles() {
return roleRepository.SuperADMINRoles();
}
public List<Unit> getAllUnits() {
return unitRepository.findAll();
}
public void deleteUserById(String id) {
User user = userRepository.findOne(Long.valueOf(id));
userRepository.delete(user);
}
public User getAgentByNIC(String nic) {
return userRepository.getAgentByNIC(nic);
}
}
这是我的应用程序配置。
package lk.tradeportal;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.annotation.PreDestroy;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Created by ignotus on 1/22/2017.
*/
@Configuration
@ComponentScan(basePackages = "lk.tradeportal")
@EnableWebMvc
@ImportResource(locations = "classpath:tradeportal-servlet-config.xml")
@EnableJpaRepositories(basePackages="lk.tradeportal.repository")
@EnableTransactionManagement
public class TRADEPORTALStarter extends SpringBootServletInitializer {
private static final Logger slsiLogger = LogManager.getLogger(TRADEPORTALStarter.class);
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
slsiLogger.info("Starting application");
SpringApplication application = new SpringApplication(TRADEPORTALStarter.class);
context = application.run(args);
application.setRegisterShutdownHook(true);
}
@PreDestroy
private static void closeAppContext(){
context.close();
}
@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
return application.sources(TRADEPORTALStarter.class);
}
}
这是我的persistence.xml
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ogm-jpa-tutorial" transaction-type="RESOURCE_LOCAL">
<!-- Use Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>lk.tradeportal</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<!--<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />-->
<property name="hibernate.ogm.datastore.create_database" value="true"/>
<property name="hibernate.ogm.datastore.database" value="trade_portal_db"/>
</properties>
</persistence-unit>
</persistence>
我的依赖是
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<!--handle servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--<Email Dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<!--jasper-->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.7.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web-services -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ws.security/wss4j -->
<dependency>
<groupId>org.apache.ws.security</groupId>
<artifactId>wss4j</artifactId>
<version>1.6.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-libs -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-libs</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-security</artifactId>
<version>2.2.0.RELEASE</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.1.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.hql</groupId>
<artifactId>hibernate-hql-parser</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.4.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.ogm/hibernate-ogm-core -->
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-search-orm -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.6.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-search-engine -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-engine</artifactId>
<version>5.6.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.hql</groupId>
<artifactId>hibernate-hql-parser</artifactId>
<version>1.4.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
@query注释中的错误是什么。我如何从我的数据库获取数据..请帮助我。