Customer
, you do not need to use the name
attribute of the
@Table(name=CUSTOMER)
annotation to map it to the customer
table in the
database.
So naming conventions need to take into account the fact that
annotations use many default names to make coding simpler. This means
there is some implicit naming that you need to be aware of. We
recommend following all the
defaults for naming conventions of annotation as it allows you to write
less code for annotations and instead just rely on defaults.
Identifier Type |
Rules for Naming |
Examples and Reference |
Entities (annotation is @Entity) | Just use the name of the object. Do not append a "Bean" or "POJO" suffix. Follows naming conventions for classes |
For example, Address.java would be the name
of an
address
entity. |
EntityManagerFactory (annotation is @PersitenceUnit) | emf is the name if there is only one entity manager factoryIf there is more than one entity manager factory, the naming rule is to append "Emf" to the name like <persistence unit name>Emf |
For example, public EntityManagerFactory emf; For more than one, public EntityManagerFactory pestoreEmf; public EntityManagerFactory inventoryWarehouseEmf; |
EntityManager (annotation is @PersistenceContext) | em is the name if there is only one entity managerIf there is more than one entity manager, append "Em" to the name. Often an entity manager is closely related to a persistence unit, so a common convention is <persistence unit name>Em |
For example,
When more than one, private EntityManager pestoreE m; private EntityManager inventoryWarehouseEm; |
UserTransaction | utx is the name to be used |
For example,public UserTransaction utx; |
Listeners (annotation is @EntityListeners) | Rule is to append "Listener" to the entity name |
For example,OrderListener |
Persistence Unit |
Should be based on the resource
name. The
resource name should be suffixed with "Pu". The resource name should be
written in mixed case with the first letter of
each word capitalized<resource-name>Pu |
For example, in the persistence.xml file<persistence> |
DataSource name |
Should be based on the resource
name. The
resource name should be written in mixed case with the first letter of
each word capitalized |
Examples: jdbc/PetStoreDB |
O/R mapping
file This file is used when wanting to override O/R mapping annotations in the source code. |
Use the default name orm.xml
when only one mapping file is needed per
META_INF/persistence.xml.When more than one mapping file is needed , use <resource-name>Orm .xml |
For example,<persistence> |
Named queries and
finder methods |
In mixed case with
the first letter lowercase, with the first letter of each internal word
capitalized. Named queries must be scoped. We use the class name as the scope. |
For example,
"getItemsPerProductCategory" in this named query@NamedQuery( Then in a query that uses the named query, it would scope it and use it like this Query query =
|
Named parameters | In mixed case with the first letter lowercase, with the
first letter of each internal word capitalized. |
In the Java Query language you can specify named
parameters that can be
passed in to queries. These named parameters are also used by named
queries. For example, the parameter ":categoryName" query = SELECT i FROM Item i
WHERE i.product.categoryId LIKE :categoryName "); categoryName",
"dogs"); |
Database |
If your organization already has a convention for database naming, follow that. Otherwise, name databases after the application and append DB. <application-name>DB |
For example: PetStoreDB |
Customer
. In this case, you would need
to put
the mapping information to CUSTOMERS in the entity annotation for the
Customer entity. As this is a common practice, we will consider this an
alternative
convention, though it requires that each entity class use the table
annotation to map the entity to the table, as for example with
@Table(name=CUSTOMERS) annotation on the Customer entity.