Implement a GenericRepository in SpringData JPA with Hibernate? - spring-data-jpa

I have more than 20 entitity classes. I just don't want to generate JPA Repository for each of them. Instead a generic repository needs to be implmented
for normal crud operations like save(), findAll(), findOne().
For ex: -
#Entity
class Student{}
#Entity
class Teacher{}
public interface GenericRepository<T,Integer> extends JpaRepository{ }
Here T should hold any type of Entity.. like Student or Teacher.

Related

JPA same table with two implementing classes

I have an entity Product which contains another entity Category.
public class Product implements Serializable {
...
private Category category;
}
However, mobile products need a special kind of category.
public class MobileProduct extends Product implements Serializable {
...
private MobileCategory mobileCategory;
}
It makes sense to make MobileCategory inherit Category because in this case you won't need two classes for products but if you want to create queries using MobileCategory attributes, JPA may not allow that. If you choose to have two classes with a inheritance relationship, you are using exactly the same table with exactly the same attributes but with two implementing classes.
Which approach should I choose?

Entity Framework - Same field and column name in base and derived classes

I am using EF 4.3 and am adding Audit fields to my classes and tables. I have a service layer which is getting the credentials of the client applications by using the OperationContext, so I am passing that information to my tables through EF mappings. An example of this would be:
class A
{
string CreatedByUser { get; set; }
}
class B : A
{
}
I am using the fluent interface to provide my POCO to table mappings -- when I map the CreatedByUser column in the base and derived class, the derived class mappings do not take effect and the information is not passed to the database.
I have gotten around this by creating fields in my base class for the derived classes to use that are just pass-throughs of the audit columns but this is messy.
Try making class A abstract, I think that will give you the effect you are looking for(add the columns from class A to all class/tables that inherit it)

Does JPARepository use method return type to find target table?

This seems to work, but I'd like someone to confirm this:
I have a base class BaseEntity for my entities.
#Entity
#Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
...
}
I then have several entities, say A and B and C that extend from BaseEntity.
In addition, I have a single JPARepository defined as:
JPARepository<BaseEntity, Long>
Now it looks like I can use this repository to query tables A,B or C separately with simply changing the return type. For example:
public List<B> findBsByNameContaining(String name);
will result in a query that selects rows from table B only.
Is this really how it works?
If you provide a subtype of the type you declared at the repository interface level we will use that to execute the query against (read: essentially trigger em.createQuery(typeReturnedByTheMethod)). In case the returned type is not a subtype of the domain class managed by the repository, we'll still use the plain domain type.

Hibernate component

I need to migrate some hibernate hbm files to JPA/Hibernate annotations.
The existing relationship is as follows -
The parent class has an ID
The component class also has an ID
The 2 identifiers refer to different sequences.
I have used #Embedded and #AttributeOverride in the parent class, and #Embeddable in the component class.
Both the classes are entities.
The exception i get when i try to save a parent class object is -
org.hibernate.MappingException: component property not found: id
I suppose the exception is because i have 2 identifiers defined.
Any suggestions/workarounds on this will help greatly.
You can't make something an #Entity and #Embeddable at the same time, that makes no sense. You have to make it one or the other.
If both have an ID, and both are entities, then the Hibernate/JPA component/embeddable model doesn't apply.

extending an entity framework model

i want extend an entity framework model with a temporany attribute.
I need it only in a mvc form. I don't need save it in the db.
How can i do it?
Create a partial class for the entity you want to extend
e.g.
//must be in the same namespace as the Customer entity in the model
public partial class Customer
{
public string MyProperty{get;set;}
}
This property will be unmapped and you can fill it with data after you run a query or on materialization.
OR
Create a wrapper class for your entity which expose both the unmapped property and the mapped properties the properties you need in the view.