Reputation: 901
I am new in spring and hibernate and I got these two annotations @Entity
and @Repository
used for DAO class. As both the annotations are used for the DAO class. Then, what is the difference and when to use one of them.
Upvotes: 3
Views: 3447
Reputation: 39
@Entity annotation defines that a class can be mapped to a table, it is just a marker, like for example Serializable interface. Entity is an object representing (usually) a row in a db.
@Repository annotation defines CRUD operation on table. It is very like DAO pattern to fetch and save entities from/to storage - it represents db table.
Upvotes: 1
Reputation: 1467
@Entity
Let's say we have a POJO called Student which represents the data of a student and we would like to store it in the database.
public class Student {
// fields, getters and setters
}
In order to do this, we should define an entity so that JPA is aware of it.
So let's define it by making use of the @Entity annotation. We must specify this annotation at the class level.
@Entity
public class Student {
// fields, getters and setters
}
In most typical applications, we have distinct layers like data access, presentation, service, business, etc.
And, in each layer, we have various beans. Simply put, to detect them automatically, Spring uses classpath scanning annotations.
@Repository
@Repository annotates classes at the persistence layer, which will act as a database repository. @Repository’s job is to catch persistence specific exceptions and rethrow them as one of Spring’s unified unchecked exception.
to sum up @Entity is part of JPA Java Persistence API specification used mapping between a java POJO and an entity in relational database world and @Repository is a Spring stereotype used to annotate POJO beans than their jobs is database manipulation operations
Upvotes: 0
Reputation: 1860
The @Entity
class is the model and the @Repository
is the layer that helps you to extract the data from database. For example :
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(unique=true)
private String name;
//getters - setters
}
And the repository:
@Repository
public interface StudentRepository extends CrudRepository<Student,Long> {
public Student findByName(String name);
}
The basic CRUD operations are already provided by CrudRepository
interface so there is no need to implement them again. You can use them in a Service class like this:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
public StudentRepository studentRepository;
@Override
public List<Student> findAll()
{
return studentRepository.findAll():
}
@Override
public Student findByName(String name)
{
return studentRepository.findByName(name);
}
}
And in case you want to make custom queries like get a student by name, jpa hibernate is very smart and helps you to only define the method in the @Repository annotated interface and there is no need of any implementations. BUT there is a rule here if you want to make it work. Hibernate will look after method name like this : public Student findByName(String name);
the find and Student return type tells hibernate that it have to look for a Student, byName will tell that it have to query the database for a Student with a specific name. (The Name keyword is actually the entity attribute with capital letter ! )
But of course, if you need some more complex queries, there is the @Query annotation that will help you with that :) .
Upvotes: 3