I am learning on Spring boot to insert data into MongoDB and encountered some issues. Please give me some of your advise, thank you.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.onboard.proj</groupId>
<artifactId>onboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserController.java
#RestController
public class UserController {
#Autowired
MongoService db;
#PostMapping("/create")
public User createNew(#RequestParam("name") String name, #RequestParam("role") String role){
User obj = new User(name, role);
User newObj = db.save(obj);
return newObj;
}
}
MongoService.java
#Service
public class MongoService {
#Autowired
MongoRepository repo;
public User save(User u) {
return repo.save(u);
}
}
MongoRepository.java
public class MongoRepository {
#Autowired
private MongoTemplate template;
public User save(User e) {
if(e != null) {
template.insert(e);
}
return e;
}
}
User.java
#Document
public class User {
#Id
private String id;
//#Field("name")
private String name;
//#Field("role")
private String role;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getRole() {
return role;
}
public void setName(String name) {
this.name = name;
}
public void setRole(String role) {
this.role = role;
}
public User(String name, String role) {
super();
this.name = name;
this.role = role;
}
public User() {
super();
}
}
I tried to use PostMan and did the following setting, but couldnt reach the UserController. The result from Postman is always 404, NOT FOUND.
Add the annotation #RequestMapping("/") also on class level since Spring requires a mapping for your class also, not only the method.
#RestController
#RequestMapping("/")
public class UserController {
Also you have
#RequestParam("name") String name, #RequestParam("role") String role
but in Postman you pass name and role in JSON body. It is not coded to work like this. You should pass name and role as query parameters in Postman.
#RestController
#RequestMapping("/")
public class UserController {
#Autowired
MongoService db;
#PostMapping("/create")
public User createNew(#RequestParam("name") String name, #RequestParam("role") String role){
User obj = new User(name, role);
User newObj = db.save(obj);
return newObj;
}
}
it is better to use swagger you can get endpoints easily.
Related
#Target({TYPE, ANNOTATION_TYPE, FIELD})
#Retention(RUNTIME)
#Constraint(validatedBy = {UniqueNameValidator.class})
#Documented
public #interface UniqueName {
String message() default "Unique name constraint";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class UniqueNameValidator implements ConstraintValidator<UniqueName, Object> {
#Override
public void initialize(UniqueName constraintAnnotation) {
System.out.println("UniqueNameValidator -> initialize() method invoked!");
}
#Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
System.out.println("UniqueNameValidator -> isValid method invoked!");
return true;
}
}
#Entity
public class MyClass implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
#UniqueName
#Column(name = "NAME")
private String name;
//Getters & Setters
}
The dependencies used in my project are :
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.6.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
Env :
Java 8
WebLogic Server: 12.2.1.2.0
Eclipselink version 2.6.4
After trying many suggestions from SOF i can't figure out why my custom validator is not called before persisting the entity (Validation on class level or field level) !
Any help will be appreciated.
Code and Configuration :
DB used - H2 in Memory database
User Entity :
#Entity
#Table(name = "users")
#Data
#AllArgsConstructor
#NoArgsConstructor
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 455202739064202185L;
#Id // please note is not auto generated but manually generated
#Column(name = "id",unique=true)
#NotNull
private long id;
#Column(name = "firstName")
#NotEmpty(message = "First name is required")
private String firstName;
#Column(name = "lastName")
#NotEmpty(message = "Last name is required")
private String lastName;
#Column(name = "email")
#NotEmpty(message = "Email is required")
#Email(message="Email Must be well formed")
private String email;
}
UserRepository :
public interface UserJpaRepository extends JpaRepository<User, Long> {
}
UserServiceImpl :
this.userJpaRepository.saveAll(users);
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exostar</groupId>
<artifactId>FileUpload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FileUpload</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
</project>
Issue :
As per the User entity class , #Id is manually generated.
(I have a CSV file which has Id field which is read in User entity)
So Id is not auto generated
Whenever I call this.userJpaRepository.saveAll(users) and even when there is an User entity with Id already existing , the User entity gets overwritten.
That is , Id with same entity gets overwritten with new data.
It has to throw - Unique index or primary key violation error
However, when I try to insert duplicate entry into H2 database MANUALLY , it throws exception - Unique index or primary key violation
I also tried adding unique=true to Id field in User Entity but its not working
Can anybody please help on resolving this issue please ?
Well as it turns out that the behavior that you are seeing is because of the call to em.merge(...) in the save() implementation. Here is what actually happens:
#Transactional
#Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be null.");
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity); // Update if the entity is not new
}
}
This is a sample code from SimpleJpaRepository implementation. you can use your own query methods for updating the existing query something like this:
#Modifying
#Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
I'am new to micronaut and I try to follow this little project. However I would like it to work with postgres.
My application.yml looks like this:
micronaut:
application:
name: hello-world
datasources:
default:
url: 'jdbc:postgresql://localhost:5432/test'
username: test
password: test
driver-class-name: org.postgresql.Driver
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
I have access to the database via intellij.
In the pom.xml I have the following dependencies:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-jpa</artifactId>
</dependency>
Additionally it is mentioned in the link that one needs annotionProcessor, so I added this to my build profile:
<annotationProcessorPaths>
<path>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</path>
</annotationProcessorPaths>
So now everytime I try to do the following:
#PersistenceContext
private EntityManager entityManager;
I get the following error:
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
However I already have annotation processing enabled. And I also have a #Entity-class:
#Entity
#Table(name = "users")
public class User {
#NotBlank
#Column(name = "name")
private String userName;
public User() {
}
public User(#NotBlank final String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(final String userName) {
this.userName = userName;
}
}
What exactly am I missing in my setup?
There were multiple issues with my setup.
datasource and jpa need to be at root level
The #Entity needs a #Id
So in the end the application.yml looks like this:
micronaut:
application:
name: hello-world
datasources:
default:
url: 'jdbc:postgresql://localhost:5432/test'
username: test
password: test
driver-class-name: org.postgresql.Driver
jpa:
default:
packages-to-scan:
- 'test'
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
And the #Entity-Class needs an #Id-Attribute:
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotBlank
#Column(name = "user_name")
private String userName;
public User() {
}
public User(#NotBlank final String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(final String userName) {
this.userName = userName;
}
#Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
'}';
}
}
Below is pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>DemoMongoDBProduc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DemoMongoDBProduc</name>
<description>Say Hello To Boot Using Mongo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
// **Below is my main method**
package com.mono.mongo.crud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoMongoDbProducApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMongoDbProducApplication.class, args);
}
}
// Below is my Controller class
package com.mono.mongo.crud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.service.PersonService;
#RestController
public class PersonController {
#Autowired
private PersonService personService;
#RequestMapping("/create")
public String create(#RequestParam String firstName, #RequestParam String lastName, #RequestParam int age) {
Person p = personService.create(firstName, lastName, age);
return p.toString();
}
#RequestMapping("/get")
public Person getPerson(#RequestParam String firstName) {
return personService.getByFirstName(firstName);
}
#RequestMapping("/getAll")
public List<Person> getAll(){
return personService.getAll();
}
#RequestMapping("/update")
public String update(#RequestParam String firstName, #RequestParam String lastName, #RequestParam int age) {
Person p = personService.update(firstName, lastName, age);
return p.toString();
}
#RequestMapping("/delete")
public String delete(#RequestParam String firstName) {
personService.delete(firstName);
return "Deleted : "+firstName;
}
#RequestMapping("/delete")
public String deleteAll() {
personService.deleteAll();
return "Deleted all records";
}
}
// Below is my Service class
package com.mono.mongo.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.repository.PersonRepository;
#Service
public class PersonService {
#Autowired
private PersonRepository personRepository;
// Create operation
public Person create(String firstName, String lastName, int age)
{
return personRepository.save(new Person(firstName, lastName, age));
}
// Retrieve operation
public List<Person> getAll()
{
return personRepository.findAll();
}
public Person getByFirstName(String firstName)
{
return personRepository.findByFirstName(firstName);
}
// Update Operation
public Person update(String firstName, String lastName, int age)
{
Person p = personRepository.findByFirstName(firstName);
p.setLastName(lastName);
p.setAge(age);
return personRepository.save(p);
}
// Delete Operation
public void deleteAll() {
personRepository.deleteAll();
}
public void delete(String firstName) {
Person p = personRepository.findByFirstName(firstName);
personRepository.delete(p);
}
}
// Below is my PersonRepository class
package com.mono.mongo.crud.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import com.mono.mongo.crud.model.Person;
#Repository
public interface PersonRepository extends MongoRepository<Person, String> {
public Person findByFirstName(String firstName);
public List<Person> findbyAge(int age);
// public Person findByFirstName(String firstName);
// public List<Person> findByAge(int age);
}
// Below is my Service class
package com.mono.mongo.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mono.mongo.crud.model.Person;
import com.mono.mongo.crud.repository.PersonRepository;
#Service
public class PersonService {
#Autowired
private PersonRepository personRepository;
// Create operation
public Person create(String firstName, String lastName, int age)
{
return personRepository.save(new Person(firstName, lastName, age));
}
// Retrieve operation
public List<Person> getAll()
{
return personRepository.findAll();
}
public Person getByFirstName(String firstName)
{
return personRepository.findByFirstName(firstName);
}
// Update Operation
public Person update(String firstName, String lastName, int age)
{
Person p = personRepository.findByFirstName(firstName);
p.setLastName(lastName);
p.setAge(age);
return personRepository.save(p);
}
// Delete Operation
public void deleteAll() {
personRepository.deleteAll();
}
public void delete(String firstName) {
Person p = personRepository.findByFirstName(firstName);
personRepository.delete(p);
}
}
// Below is my POJO class
package com.mono.mongo.crud.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document
public class Person {
#Id
String id;
String firstName;
String lastName;
int age;
public Person(String firstName, String lastName, int age)
{
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person FirstName: "+firstName+" LastName: "+lastName+" age: "+age;
}
}
I am using spring tool suite as IDE, MongoDB, Maven 3.2. I am trying to learn CRUD operation using MongoDB. But when I am running this code as Spring Boot App I am getting
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personController': Unsatisfied dependency expressed through field 'personService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personService': Unsatisfied dependency expressed through field 'personRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findbyAge found for type Person!
Can some one tell me where and what mistake I am doing
Here is the screenshot of the error I am getting:
In your Repository class.
Rename interface method from
findbyAge(int age);
to
findByAge(int age);
I have resolved the issue. It was quite simple and I was doing a silly mistake.
In the Controller class for delete() method and deleteAll() method the #RequestMapping was delete like #RequestMapping("/delete") only, instead I have changed it to #RequestMapping("/deleteAll") for complete deletion and #RequestMapping("/delete") for the deleteByFirstName. And it worked for me.
I wrote a Spring boot,hibernate orm application using mysql database.... But now I want to switch to mongodb... I used Hibernate-ogm for fulfill my requirement. When run the application the console shows successfully switch to mongodb using hibernate-ogm... But the query is not working... I import below packages for my repository to hibernate-orm and these imports works fine with mysql... But in hibernate-ogm #query annotation doesn't work...
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Query;
This is my model
#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;
}
This is my full repository
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);
}
This is my Service class
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);
}
}
This is my application config.
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);
}
}
This is my 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>
My dependencies are
<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>
What is the error in #query annotation. How I get I data from my database.. Please help me.