Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

asked on

Hibernate Inheritance Strategy for Abstract Base class and two concrete classes

Friends

In order to capture the logging information of my application to the tables , I am designing three tables as shown in the attached picture.
User generated image
LogType ( Table is the Static Table ) which has only two columns and two rows fixed Id = 1 , 2 and Type = Info , Debug

INFOLOG( Table ) and DEBUGLOG Tables will have additional respective information .

My Question is which Inheritance Strategy is best suited for such scenario , I think of Table for Subclass since My super table(Log Type) is constant and wont change . Please put comments with some annotation examples .
My abstract Base class looks like below


@Inheritance(strategy=InheritanceType.JOINED) 
@MappedSuperclass 
public abstract class LogType implements Serializable 
{ 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "LogTypeId", nullable = false, updatable = false) 
private Integer logTypeId; 

@Column(name = "LogTypeName", unique = true, nullable = false, length = 10) 
private String logTypeName; 
}

Open in new window



However I also think that since my base abstract class has only two static values for column LogTypeName i.e Info and Debug

I can get rid of above abstract base class by introducing enum instead of base class as shown below

public enum LogType{
 INFO,
 DEBUG;
 }

Open in new window


Which can represent a column name in my two concrete tables as

@Entity 
public class InfoLog implements Serializable 
{ 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "InfoLogId", nullable = false, updatable = false) 
private Integer infoLogId; 

@Enumerated(EnumType.STRING)
@Column(name="LogType")
private LogType logType;

@Column(name = "LogDesc", nullable = false, updatable = false) 
private String logDesc; 

}

Open in new window


@Entity 
public class DebugLog implements Serializable 
{ 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "DebugLogId", nullable = false, updatable = false) 
private Integer infoLogId; 

@Enumerated(EnumType.STRING)
@Column(name="LogType")
private LogType logType;

@Column(name = "DebugDesc", nullable = false, updatable = false) 
private String debugLogDesc; 

}
 

Open in new window


However my questions are

1.  Should I go for a abstract base class for my LogType and then use Single table per subclass
2. Should I go for a enum instead of abstract base class as shown above and then just dont use any Inheritance .

Please suggest with examples
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ronan_40060

ASKER

Thanks Doug , I thought so , even if I dont use just entity classes and create DB tables and use just plain JDBC template I hope I can still gte my job done .
I will post a new question later today . Thank you for your time and comment.
Thank you