0

I have a repo, an entity, domain and controller. Intelliji will not compile with the error being to do with LicenseAssignmentEntity createdEntity = new icenseAssignmentRepository.save(createEntity); I am unsure as to what I am doing wrong. Thank you

package com8.myname.service;
import com8.markmcilwrath.domain.LicenseAssignment;
import com8.markmcilwrath.domain.entity.LicenseAssignmentEntity;
import com8.markmcilwrath.domain.entity.LicenseEntity;
import com8.markmcilwrath.domain.entity.UserEntity;
import com8.markmcilwrath.repository.LicenseAssignmentRepository;
import javassist.NotFoundException;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.UUID;

@Service
public class LicenseAssignmentService {
private LicenseAssignmentRepository licenseAssignmentRepository;
private LicenseService licenseService;
private UserService userService;

public LicenseAssignmentService (LicenseAssignmentRepository licenseAssignmentRepository, LicenseService licenseService,
                                 UserService userService)
{
    this.licenseAssignmentRepository = licenseAssignmentRepository;
    this.licenseService = licenseService;
    this.userService = userService;
}

public LicenseAssignment save (String licenseKey, String userID, LocalDate assignmentDate, Boolean approved)
{
    LicenseEntity licenseEntity = null;
    try {
        licenseEntity = getLicenseEntity(licenseKey);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }

    UserEntity userEntity = null;
    try {
        userEntity = getUserEntity(userID);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    
    LicenseAssignmentEntity createEntity = new LicenseAssignmentEntity(UUID.randomUUID().toString(),
             licenseEntity,  userEntity,  assignmentDate, approved);
    LicenseAssignmentEntity createdEntity = new licenseAssignmentRepository.save(createEntity);
    return new LicenseAssignment(createdEntity.getAssignmentID(), createdEntity.getLicenseEntity().getLicenseKey(),
            createdEntity.getUserEntity().getUserId(), createdEntity.getAssignmentDate(), createdEntity.getApproved());
}

private  LicenseEntity getLicenseEntity(String licenseKey) throws NotFoundException
{
    return licenseService.getLicenseEntity(licenseKey);
}

private UserEntity getUserEntity (String userID) throws NotFoundException
{
    return userService.getUserEntity(userID);
}

}

2 Answers2

0

You can save the data to the repository by following way. Suppose that you have to save post using any spring boot This will be Post Service Interface -

public interface PostService {
    public void save(Post thePost);
}

This my PostRepository Interface

public interface PostRepository extends JpaRepository<Post, Integer> {

}

In the PostServiceImpl class write the logic to save data to repository by calling the save method. First, i am performing constructor injection to add repository type object and then calling the save method of JpaRepository.

@Service
public class PostServiceImpl implements PostService {

    private PostRepository postRepository;

    @Autowired
    public PostServiceImpl(PostRepository thePostRepository) {
        this.postRepository = thePostRepository;

     @Override
    public void save(Post thePost) {
        postRepository.save(thePost);
    }

    }





Nisha Jha
  • 64
  • 3
0

The issues was the "new" in

LicenseAssignmentEntity createdEntity = new licenseAssignmentRepository.save(createEntity);

it wasn't needed, sorry