-1

Suppose I would like to have a method, which is obtaining super main customer, which has id=0.

I have Customer class:

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

According to dox, I should create two additional classes/interfaces.

So I have CustomerCustom interface:

public interface CustomerCustom {

   Customer getVeryMainCustomer();

}

where a method getVeryMainCustomer declared.

Then my exposure repository interface turns following:

public interface CustomerRepository extends CrudRepository<Customer, Long>, CustomerCustom {

    List<Customer> findByLastName(String lastName);
}

it extends both CrudRepository and my CustomerCustom.

But next I should implement CustomerCustom. But how to do that?

I wrote

public class CustomerCustomImpl implements CustomerCustom {

   @Override
   public Customer getVeryMainCustomer() {
      return null;
   }


}

bot don't know, what to write in implementation. How to reach customer?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Dims
  • 47,675
  • 117
  • 331
  • 600
  • What is the method supposed to do? Did you think about using `@Query`? What is the table structure or `Customer`? –  Dec 21 '15 at 17:01
  • Table structure of Customer is defined by JPA automatically on the basis of `Customer` class definition. The method is supposed to select the customer with ID = 0. Yes, I would like to use `@Query` if it is possible – Dims Dec 21 '15 at 17:17

2 Answers2

1

With @Query:

public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);

    @Query("SELECT c FROM Customer c WHERE c.id = 0")
    Customer getVeryMainCustomer();
}

Spring-data will handle the implementation for you using the query from the annotation.

  • But this is inside extending interface. Is it possible to use `@Query` in my custom repository implementation? – Dims Dec 21 '15 at 17:31
  • I'm sorry but I don't understand, why would you still need a custom repository impl with this solution? –  Dec 21 '15 at 17:33
  • Ok, you are right, per stated question this is sufficient. But suppose I just want both to implement repository and add some queries? – Dims Dec 21 '15 at 17:46
  • Then use @Query in the interface for spring and do your stuff in your impl as you did. –  Dec 21 '15 at 18:07
1

You need to inject entitymanager in the class for your queries

public class CustomerCustomImpl implements CustomerCustom {

    //This is my tip, but not a must...
    @PersistenceContext
    private EntityManager em;

    public void save(...){
        //do what you need here

       Employee employee = em.find(Employee.class, 1);// example of employye
    }
}

http://www.objectdb.com/java/jpa/persistence/retrieve

reos
  • 8,766
  • 6
  • 28
  • 34