Generate Getter/Setter with different visiblity
DB Visual ARCHITECT(DB-VA) generates properties (Getter/Setter) for each attribute for ORM-Persistable class. By default, the property visibility is generated as public and you can change the property visibility by following the attribute model’s visibility. In this article, you will learn how to change the generation option for property visibility, either Public or Follow attribute.
Configuring Property Visibility
DB-VA generates persistent classes based on the class model defined on class diagram. The property (Getter and Setter) for each attribute will be generated automatically. The attribute visibility must be private in persistable class but you can set the property visibility to Follow attribute or Public visibility.
In class diagram, the following symbols: +, -, #, and ~ are used to represent the public, private, protected, and package visibility of the attribute respectively.
public – accessible by any other class anywhere.
private – accessible only within the class; even methods in subclasses in the same package do not have right to access the attribute.
protected – accessible by the package classes and any subclasses that are in other packages.
package – accessible to classes in the same package but not the classes in other packages, even if they are subclasses.
Let’s try the following steps to configure the propety visibility:
Create a Class Diagram
Create an ORM-Persitable Class called
Customer with the following attributes:
| Visibility | Name | Type |
| public | name | String |
| protected | age | int |
| package | address | String |
| private | gender | char |
Click
Tools > Object-Relational Mapping (ORM) > Synchronize to Entity Relationship Diagram from the menu to synchronize the class diagram to ERD.
The Entity Relationship Diagram (ERD) is created.
Select
Tools > Object-Relational Mapping (ORM) > Generate Code... to open
Database Code Generation dialog box.
Click
Advance Settings button to open
Advance Settings dialog box.
You can select
Default(Public/Follow attribute),
Public or
Follow attribute from the drop-down menu of
Getter/Setter Visibility.
The
Default(Public/Follow attribute) option of
Getter/Setter Visibility can be changed from the application Options.
Select
Tools > Options… to open
Options dialog box.
Select
ORM and configure the
Getter/Setter Visibility to
Public or
Follow Attribute and click
OK to confirm the setting.
The Default value for
Advance Settings dialog box will be updated according to the changes in default options in the application.
After configured the Getter/Setter Visibility option, you can configure the other options for code generation, such as the output path and click OK to start generating the persistent classes and library on Database Code Generation dialog box.
Generated Code
When you select Public or Allow attribute for Getter/Setter Visibility. The visibility of the getter and setter methods will be generated based on your selection. The following are the samples of generated code include Java and C#.
public - Getter/Setter Visibility
Java
package test;
/**
* ORM-Persistable Class
*/
public class Customer {
...
private String name;
private int age;
private String address;
private char gender;
private int ID;
public void setName(String value) {
this.name = value;
}
public String getName() {
return name;
}
public void setAge(int value) {
this.age = value;
}
public int getAge() {
return age;
}
public void setAddress(String value) {
this.address = value;
}
public String getAddress() {
return address;
}
public void setGender(char value) {
this.gender = value;
}
public char getGender() {
return gender;
}
...
}
C#
namespace test {
/// <summary>
/// ORM-Persistable Class
/// </summary>
public class Customer {
public Customer() {
}
...
private string __name;
private int __age;
private string __address;
private char __gender;
private int __ID;
public string Name {
set {
this.__name = value;
}
get {
return __name;
}
}
public int Age {
set {
this.__age = value;
}
get {
return __age;
}
}
public string Address {
set {
this.__address = value;
}
get {
return __address;
}
}
public char Gender {
set {
this.__gender = value;
}
get {
return __gender;
}
...
}
Follow attribute - Getter/Setter Visibility
Java
package test;
/**
* ORM-Persistable Class
*/
public class Customer {
...
private String name;
private int age;
private String address;
private char gender;
private int ID;
public void setName(String value) {
this.name = value;
}
public String getName() {
return name;
}
protected void setAge(int value) {
this.age = value;
}
protected int getAge() {
return age;
}
void setAddress(String value) {
this.address = value;
}
String getAddress() {
return address;
}
private void setGender(char value) {
this.gender = value;
}
private char getGender() {
return gender;
}
...
}
C#
namespace test {
/// <summary>
/// ORM-Persistable Class
/// </summary>
public class Customer {
public Customer() {
}
private string __name;
private int __age;
private string __address;
private char __gender;
private int __ID;
public string Name {
set {
this.__name = value;
}
get {
return __name;
}
}
protected int Age {
set {
this.__age = value;
}
get {
return __age;
}
}
string Address {
set {
this.__address = value;
}
get {
return __address;
}
}
private char Gender {
set {
this.__gender = value;
}
get {
return __gender;
}
}
...
}
Downloads
Related Articles
Resources