I am a beginner in programming and I am learning Java.
I'm building a CRUD application with spring MVC as an exercise, and I don't have a textbook for spring, I'm learning Spring by looking at textbooks for JavaEE.
The following is a quote from the textbook, but I would like to rewrite it into Spring.
How can I rewrite JavaEE to Spring?
I don't know how to write in Spring, especially around #EJB and #Inject.
The repository and service were written by me.
//Author entity
1 id
2 autherName
3 birthPlace
#Named
#ViewScoped
public class BookshelfService implements Serializable {
private Integer id;
private String autherName;
private String birthplace;
#EJB
Bookshelf bs;
#Inject
transient Logger log;
public String create() {
Author author = new Author (id , autherName,birthplace);
return null;
}
public String edit(Author author) {
id = author.getId();
autherName = author.autherName();
Birthplace = author.getBirthplace();
return null;
}
// getter setter...
}
#Stateless
public class Bookshelf {
#PersistenceContext
private EntityManager em;
public void create(Author author) {
em.persist(author);
}
}
//written by me
#Repository
public interface AutherRepository extends JpaRepository <AutherEntity, Long> {
}
//written by me
#Service
#Transactional
public class BookService {
#Autowired
private AutherRepository autherRepository;
private BookTitleRepository booktitleRepository;
}
Related
I am new to spring.
I just tried successfully using an entity class without #Id in Spring Data JDBC
Custom query was added in my repository for retrieving data from 2 mysql tables and returning an entity having the joined table data.
If I plan to use only custom queries, am I missing anything here?
Here's my entity class without #Id or #Entity:
public class Item
{
private long id;
private String code;
private String itemName;
private String groupName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
Repository layer:
#Repository
public interface ItemRepository extends PagingAndSortingRepository<Item, Long>
{
#Query("SELECT a.id, a.code, a.name AS item_name,
b.name as group_name from item a, item_group b
WHERE a.group_id = b.id AND a.id=:id")
Item findItemById(#Param("id") Long id);
}
Service layer:
#Service
public class ItemServiceImpl implements ItemService
{
private final ItemRepository itemRepository;
public ItemServiceImpl(ItemRepository itemRepository)
{
this.itemRepository = itemRepository;
}
#Override
#Transactional(readOnly=true)
public Item findItemById(Long id)
{
return itemRepository.findItemById(id);
}
}
My updated main Configuration class in response to answer of Jens:
#SpringBootApplication
#EnableJdbcRepositories
public class SpringDataJdbcApplication extends AbstractJdbcConfiguration
{
public static void main(String[] args)
{
SpringApplication.run(SpringDataJdbcApplication.class, args);
}
#Bean
#ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource()
{
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
return dataSourceBuilder.build();
}
#Bean
NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource)
{
return new NamedParameterJdbcTemplate(dataSource);
}
#Bean
PlatformTransactionManager transactionManager()
{
return new DataSourceTransactionManager(dataSource());
}
}
If you don't get any exceptions you should be fine. There shouldn't be anything in Spring Data JDBC that silently breaks when the id is not specified.
The problem is though: I don't consider it a feature that this works, but just accidental behaviour. This means it might break with any version, although replacing these methods with custom implementations based on a NamedParameterJdbcTemplate shouldn't be to hard, so the risk is limited.
The question though is: Why don't you add the #Id annotation, after all your entity does have an id. And the whole idea of a repository conceptually requires an id.
If it's working and you really don't want to use the annotations, you can do it. But I think that it's unnecessary complication. You can expect errors that would not be there if you had used the annotations and code will be harder to debug. If you are new in Spring I recommend to use annotations. But after all it depend on you how will you design your applications. For sure advantage of approach without annotations is higher control about database.
I am new to setting up embedded Elasticsearch into my Spring Boot application where Spring Data JPA is setup with Postgres database.
Now I've also added support for Elastic Search Spring Data repositories (or so I thought). The problem is that ES searches do not return anything (JSON array is empty), whilst JPA ones work correctly.
I read that people need an index tool that runs every now and then, but I couldn't find anything related to this in the Spring Data Elastic Search documents.
Do I understand correctly that you need to index the searches from the database constantly? Is the answer provided in this topic Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch the only solution:
Here is the Application class:
#SpringBootApplication
#EnableJpaRepositories(basePackages = "eu.deniss.repository")
#ComponentScan
public class SpringDataElasticsearchDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataElasticsearchDemoApplication.class, args);
}
}
Person Entity class:
#Entity
#Document(indexName = "person", type = "person")
public class Person {
private Long id;
private String firstName;
private String lastName;
private String email;
private String gender;
private String ipAddress;
#Id
#org.springframework.data.annotation.Id
#Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
}
PersonSearchRepository class:
public interface PersonSearchRepository extends ElasticsearchRepository<Person, Long> {
}
PersonServiceImpl class:
#Service
public class PersonServiceImpl implements PersonService {
private final PersonRepository personRepository;
private final PersonSearchRepository personSearchRepository;
private final PersonMapper personMapper;
private static final Logger log = Logger.getLogger(PersonServiceImpl.class);
public PersonServiceImpl(PersonRepository personRepository, PersonSearchRepository personSearchRepository, PersonMapper personMapper) {
this.personRepository = personRepository;
this.personSearchRepository = personSearchRepository;
this.personMapper = personMapper;
}
...
#Override
#Transactional(readOnly = true)
public Page<PersonDTO> search(String query, Pageable pageable) {
log.info("Request to search for a page of People with a query " + query);
Page<Person> result = personSearchRepository.search(queryStringQuery(query), pageable);
return result.map(person -> personMapper.personToPersonDTO(person));
}
}
PersonController class:
#RestController()
#RequestMapping("/api")
public class PersonController {
private final PersonService personService;
private final Logger log = LoggerFactory.getLogger(PersonController.class);
private static final String ENTITY_NAME = "person";
public PersonController(PersonService personService) {
this.personService = personService;
}
#GetMapping("/_search/people")
public ResponseEntity<List<PersonDTO>> searchPeople(#RequestParam String query, Pageable pageable) throws URISyntaxException {
log.info("REST request to search for a page of Appointments for query {} ", query);
Page<PersonDTO> page = personService.search(query, pageable);
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/people");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
}
The answer was quite simple.
I had to do a batch job of querying current entities from a database and saving them to Elastic Search by using
personSearchRepository.save(person);
I do everything according to http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html, but neither in-bean methods nor external ones are ever executed. What might be the cause?
#Entity
#EntityListeners(EntityListener.class)
public class User {
#Id
#Column
#GeneratedValue
private Integer id;
// etc...
#PostConstruct
#PostLoad
#PostPersist
#PostUpdate
public void magic() {
System.out.println("YES I AM EXECUTED!");
System.exit(123);
}
}
OR
#Entity
#EntityListeners(MyListener.class)
public class User {
#Id
#Column
#GeneratedValue
private Integer id;
// etc...
}
+
public class MyListener {
#PostPersist
void postPersist(Object object) {
System.out.println("CAN'T BELEIVE I SEE THIS!");
System.exit(234);
}
}
My code creates, saves and loads beans, but nothing happens on the listeners. This is a piece of the repository thats perform the operations:
#Repository
public class UserRepositoryImpl implements UserRepository {
#Autowired
private SessionFactory sessionFactory;
#Override
public User get(Integer id) {
return sessionFactory.getCurrentSession().get(User.class, id);
}
#Override
public User save(User user) {
Session session = sessionFactory.getCurrentSession();
user = (User) session.merge(user);
session.saveOrUpdate(user);
return user;
}
// etc...
}
Repository methods are called from services like this one:
#Service
#Transactional
public class UserServiceImpl implements UserService {
#Autowired
private UserRepository userRepository;
#Override
public void something() {
// just some repo calls + extra logic
}
}
I do not think I got something special here.
JPA interceptors mechanism work only when you manipulate entities via JPA EntityManager, they have no effect when you're using Hibernate Session directly.
You'll have to implement Hibernate native interceptors if you want to use the Session API.
I want suggestion regarding a scenario I've been thinking of doing if possible. Suppose I have some JPA database entity class like:
#Entity
public class Person {
#Column(name = "ID")
private Long id;
#Column(name = "FIRST_NAME")
private String firstName;
#Column(name = "LAST_NAME")
private String lastName;
public String getFirstName(){
return this.firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return this.lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
}
I am using EJB services. I can use separate business logic methods to make CRUD operation over these entities. Is it possible to use a generic template CRUD operations for these entity classes? Like if I want to create new person I will provide the Person entity class and fields to set as parameter and my generic method will create a new Person record and will do the same job for Read, Update and Delete operation as well.
Any respective example will be highly appreciated.
Thank You
Using EJB and JPA
You can consider an abstract class for the service layer:
public abstract class AbstractFacade<E extends Serializable,
PK extends Serializable> {
private final transient Class<E> entityClass;
public AbstractFacade(final Class<E> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(final E entity) {
final EntityManager entityManager = getEntityManager();
entityManager.persist(entity);
}
public final E find(final PK id) {
return getEntityManager().find(entityClass, id);
}
// Other common operations
}
And a particular service:
#Stateless
public class PersonFacade extends AbstractFacade<Person, Long> {
#PersistenceContext(unitName = "MyPU")
private EntityManager em;
#Override
protected EntityManager getEntityManager() {
return em;
}
public PersonFacade() {
super(Person.class);
}
// Other methods of this service
}
Using Spring and Hibernate
You could have a abstract base class for common DAO methods.
public abstract class AbstractDAO<E extends Serializable,
PK extends Serializable> {
private final transient Class<E> entityClass;
public AbstractDAO(final Class<E> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public final E find(final PK id) {
return getEntityManager().find(entityClass, id);
}
// Another common methods
}
In every DAO implementation, you can put particular methods for that DAO.
#Repository
public final class PersonDAO extends AbstractDAO<Person, Long> {
#Autowired
private transient EntityManagerFactory emf;
public PersonDAO() {
super(Person.class);
}
#Override
protected EntityManager getEntityManager() {
return emf.createEntityManager();
}
// particular methods for this DAO
}
What about if the user not exists? Put this logic in the service layer.
#Service
public final class PersonService {
private static final Logger LOG = LoggerFactory.getLogger(PersonService.class);
#Autowired
private transient PersonDAO personDAO;
public Person findPerson(final Long id) {
return personDAO.find(id);
}
}
If you are using Spring then use Spring Data which will do all this for you.
http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/repositories.html#repositories.core-concepts
I have two tables Employee and Address as shown:
public class Employee {
private Integer id;
private String firstName;
private String lastName;
private boolean employeeStatus;
private Address address;
//getters setters
}
public class Address {
private Integer id;
private String country;
private String city;
private String street;
private Integer emp_id;
//getters setters
}
#Repository("employeeDao")
public class EmployeeDaoImpl implements EmployeeDao {
private JdbcTemplate jdbcTemplate;
#Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
#Override
public void insertEmployee(Employee e)
{
String sql = "INSERT INTO tbl_employee (dept_id,firstName,lastName,employeeStatus) values(?,?,?,?,?)";
this.jdbcTemplate.update(sql,new
Object[]{e.getDept_id(),e.getFirstName(),e.getLastName(),e.isEmployeeStatus()});
// INSERT ADDRESS????
}
// Other Methods
}
Now i want to implement Transactional while inserting the employee and address table attributes. I am abit confused here. Does #transactional annotation over the method does the required job? So far i understood that. Also, is it best practice to insert address from where i am inserting employee attributes? I also read somewhere that the transactional should be implemented from service layer than Dao. How would transactional can be implemented in this case?
EDIT
Since it is recommended to use #transactional in service layer, service layer became like:
#Service("employeeService")
#Transactional
public class EmployeeServiceImpl implements EmployeeService{
#Autowired
EmployeeDao employeeDao;
#Autowired
AddressDao addressDao;
#Override
public void insertEmployee(Employee e) {
employeeDao.insertEmployee(e);
addressDao.insertAddress(e.address);
}
}
is it the right way to perform transactional? Also can anyone explain #Transactional(propagation = Propagation.SUPPORTS, readOnly = true) instead of plain #Transactional ?
Alltough the #Transactional annotation would do the job, transactions are usually defined on service level. This way one business call is in one transaction, making sure everything succeeds or fails together.
you can read about #transactional in combination with jdbctemplate here