Naming convention for use when converting from database to java variables

I am currently working on a project in which I convert database information into java objects, and I was curious how I should represent / name objects and variables on the java side.

For example, if I convert a table named "Dog" containing information about such dogs as: Column: BREED_C, NAME_C, OWNER_C, DOGID_D, HAS_RABIES_I , etc. In a Java object called Dog with corresponding variables, should I follow a java naming convention like BreedC , or use Breed_C or even Breed_C so that there is a slight discrepancy between the two systems?

+4
source share
4 answers

If you work in Java, use the Java naming conventions.

It looks like your code should be responsible for abstracting the database layer from the rest of the application, so there is no reason to disclose the database view by naming Java variables with the same name as the database columns.

+6
source

I think that C, D and I are column types that are not needed in Java because you have types for fields and getters / seters.

At the beginning of this, if possible, use the Java lowerCamelCase convention.

All of the well-known Java database abstraction (JPA) projects go this way.

+1
source

According to code conventions for the Java programming language, for class names

Class names must be nouns in the mixed case with the first letter, each inner word is capitalized. Try to keep simple class names and descriptive. Use whole words - avoid abbreviations and abbreviations (if the abbreviation is much more widely used than a long form, such as URL or HTML).

And variable names

With the exception of variables, all instance, class, and class constants in the mixed case with a lowercase letter. Inner words begin with capital letters.

You can use these methods to convert database names to Java names.

 public static String toJavaFieldName(String name) { // "MY_COLUMN" String name0 = name.replace("_", " "); // to "MY COLUMN" name0 = WordUtils.capitalizeFully(name0); // to "My Column" name0 = name0.replace(" ", ""); // to "MyColumn" name0 = WordUtils.uncapitalize(name0); // to "myColumn" return name0; } public static String toJavaClassName(String name) { // "MY_TABLE" String name0 = name.replace("_", " "); // to "MY TABLE" name0 = WordUtils.capitalizeFully(name0); // to "My Table" name0 = name0.replace(" ", ""); // to "MyTable" return name0; } 

These methods use Apache Commons Lang .

+1
source

It is better to use the Java convention, so it should still be chosen differently. If you use JPA, adding @Column(name="BREED_C") to the breedC field can do the trick to combine the two systems.

EDIT: Using your example as an example:

 @Table(name="DOG") public class Dog { @Column(name="BREED_C") private String breedC; } 

If the field name matches the column name, you do not need these annotations, but if they are different, you need to put them as your case. But only if you use something like JPA.

0
source

Source: https://habr.com/ru/post/1411343/


All Articles