Reputation: 3209
I'm currently working on a project where I'm converting database info to java objects, and I was curious has to how I should represent/name the objects and variables on the java side.
For example, if I'm converting a table named Dog containing info about dogs such as:
Column: BREED_C, NAME_C, OWNER_C, DOGID_D, HAS_RABIES_I
and so on into a java object called Dog with corresponding variables, should I follow a java naming convention such as BreedC
or use Breed_C
or even BREED_C
so there's as little discrepancy between the two systems?
Upvotes: 4
Views: 3907
Reputation: 792
Better is use Java convention, so breedC should be your choice. If you're using JPA, adding @Column(name="BREED_C")
over the breedC field can make the trick to join the two systems.
EDIT: Using your case 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 don't need those annotiations, but if they're different, as your case, you need to put them. But only if you're using something like JPA.
Upvotes: 0
Reputation: 42020
Acording to the Code Conventions for the Java Programming Language, says for Class's names
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words—avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
And the variables's names
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.
You can use this methods for 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;
}
This methods using Apache Commons Lang.
Upvotes: 1
Reputation: 26333
I guess C, D and I are the types of the columns, which are not necessary in Java, because you have types for fields and getters/setters.
This begin said, use the Java lowerCamelCase convention whenever possible.
All well known Java database abstraction projects (JPA) go this way.
Upvotes: 1
Reputation: 26574
If working in Java, use Java naming conventions.
It sounds like your code should be responsible for abstracting the database layer away from the rest of the application anyway so there is no reason to expose the database representation by naming the Java variables with the exact same name as the database columns.
Upvotes: 6