Reputation: 9728
I have read this question about data types. How do I deal with such things in general and also in db? What I know/understand: compared to a regular class, two objects of a data type class should be considered equal if they have the same attribute value(s).
Example:
// Using Java here just as pseudo code. Personally, I don't use Java.
class Car {
public Color color;
}
// But here I'm not sure. Is this better?
class Color {
static String RED = 'red';
static String BLACK = 'black';
public String value; // gets a value from class constants
}
// Or this?
class Color {
static int RED = 1;
static int BLACK = 2;
public int value; // gets a value from class constants
}
And I also don't know if I need two tables (car, color) in db or just one. Should colors be hard-coded and not stored in the db?
Sorry, if I cannot describe my problem clearly. I hope anyone can provide an example. But I guess if I simply would add a Varchar column into the table so that each record gets a string 'red' or 'black' (or other colors), everyone would recommend me to use another table for the colors and make a relation between the tables. That's okay. But then again in code if I want to get the color I would always need to reference the value attribute - e.g. if I want to show the color (string) in the UI I always need to do carObj.color.value, instead of just carObj.color which would be more intuitive, IMO. ... there is something that I don't understand properly.
Can anyone show an example when and how to use data type classes/objects?
Upvotes: 0
Views: 89
Reputation: 4412
Why don't you use an enumerated type for Color?
Please see https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Upvotes: 0
Reputation: 31
Well, what they say is correct, you should represent your types in two tables. One table for the Car and one for the Color related by an ID.
//rapresent a row of your table Car, an Entity
public class CarDTO {
private int idCar;
private String type;
private int idColor;
public ...// getters and setters here
//For your first question, about if types are equals, you need to implement the method
@Override
public boolean equals(Object obj) {
if (!(obj instanceof CarDTO )) {
return false;
}
CarDTO carToCompare = (CarDTO ) obj;
return (this.getIdCar() == carToCompare.getIdCar()
&& this.getType().equals(carToCompare.getType())
&& this.getIdColor() == carToCompare.getIdColor());
}
//rapresent a row of your table Color, an Entity
public class ColorDTO {
private int idColor;
private String color;
public ...// getters and setters here
//For your first question, about if types are equals, you need to implement the method
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ColorDTO )) {
return false;
}
ColorDTO colorToCompare = (ColorDTO ) obj;
return (this.getIdColor() == colorToCompare.getIdColor()
&& this.getColor().equals(colorToCompare.getColor()));
}
You have two entities that represents the rows of your tables, and idColor of Car is related to idColor of Color. These of course are types not related in your code for now, but you can create an Entity that is the join of the informations that you need. for example the query:
SELECT c.idCar, c.type, co.colour FROM Car c, Colour co WHERE c.idColor = co.idColor
The Entity that will be the representation of your row, result of the join query is this:
public class CarAndColorDTO {
private int idCar;
private String type;
private String color;
public ...// getters and setters here
//...Equals here
}
You should avoid the hardcode of color in code, because you need these informations in your DB. Of course this is a simple explanations, if you use Java you should see JPA Entity for better explanation and easy use (Join, Foreign Keys, ecc...)
Also watch out encapsulation of your variables in the code:)
I hope this can help you
Upvotes: 1