Reputation: 149
How can I a map static table (which is not expandable) to an enum in Java and bind this table to another one?
For example, I have simple calculator web app (spring web MVC + hibernate) with one table (results of user's calculations) which has the following fields: id (PK)
, leftOperand
, operation
, rightOperand
, result
.
I would like to create a new static table (I mean for basic arithmetic operations like PLUS
, MINUS
, DIVIDE
, MULTIPLY
) with 2 fields: id(PK
) and operation
, and map this table to enum in Java.
So how can I bind these two tables (using operation
field)?
Some pseudo-code is greatly appreciated.
Note that I do not need to create a hibernate entity for the static table. Just enum.
Upvotes: 1
Views: 4089
Reputation: 2568
I think you don't need static table for operation values. Just change operation
field type to enum and use @Enumerated
.
enum Operation {
PLUS, MINUS;
}
@Entity
public class Calculation {
private String leftOperand;
@Enumerated(EnumType.STRING)
private Operation operation;
private String rightOperand;
}
Upvotes: 1
Reputation: 1105
As with classes, you can add properties to enums like so:
public enum MyEnum {
PLUS(1, "something"),
MINUS(2, "something");
private final int id;
private final String string;
private MyEnum(int id, String string){
this.id = id;
this.string = string;
}
public int getId(){
return id;
}
public String getString(){
return string;
}
}
Assuming the 'opreration' field matches the enum's name
, you can do the following:
MyEnum enumValue = MyEnum.valueOf(map.get("operation"));
Upvotes: 1