简体   繁体   中英

What is exactly a Configuration by Exception in JPA?

In one course about JPA, they say that:

JPA uses configuration by exception for default relationship mapping

In this example:

@Entity
public class Student {
    @Id
    private Long id;
    private String name;
    private Float rollno;

    public Student()
    {
    }
    //set of setters and getters
}

They tell that:

If you do not provide @Entity and @Id annotations, still Student will behave like a POJO and not be persisted.

I need some help to understand the use of the word Exception in this context.

What is Configuration by Exception in JPA?

The term "Exception" means "out of the ordinary". So, "Configuration by Exception" means that you specify a certain default way in which things will be done, and then if at a specific spot in your code you want something done differently, then you supply additional configuration at that spot only. So, essentially, only the places that require something out of the ordinary need to receive additional configuration.

So, for example, when you annotate a JPA entity with @Access( AccessType.FIELD ) you are saying that the default access mechanism to be used by JPA throughout this entity should be field access. This means that JPA should persist your class by directly accessing the fields of your class. But if at some point within your class you have a getter that you would like JPA to use instead of a field, then you can specify an additional @Access( AccessType.PROPERTY ) annotation on just that one getter, and JPA will use that getter.

Similarly, for the majority of configuration options supported by JPA, there is a default behaviour, but you always have the freedom to override this default behaviour by supplying an extra annotation on a case by case basis.

The opposite of configuration by exception would be if JPA was requiring you to specify the access type for every single field and getter, which would be very tedious, or if JPA was requiring you to specify all configuration in advance without allowing you to choose something different on a case by case basis.

'Exception' here refers to 'Exempting your class or elements of your class from default behavior'

For example: 1) With out any annotations like @Entity the above student class is a simple POJO to the persistent provider which is a default behavior.

2) On adding @Entity the Student will be treated as a persistent Entity by the persistent provider, with table name as Entity name(which is 'Student'; even this Entity name mapping to the table name is also a default behavior).

3) If you want to make an exception to the above default configuration, where you want the table name to be different from the entity name all you need to do is just to annotate with @Table to give a different table name, which you are configuring it in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM