Bi Act
Bi Act

Reputation: 434

hibernate for child entity

Quick hibernate question - How can I use Hibernate annotations to define this class as an entity -

public class NativeType {
  private long id;    
  private String name;
  private int maxPrecision;
  private byte maxScale;
  private DataStoreProvider provider;

linked as a Map Collection in the AbstractDataStoreProvider class -

public abstract class AbstractDataStoreProvider implements DataStoreProvider,
  Serializable{

  private Map<String, NativeType> uniqueNativeTypes;
  ..

public interface DataStoreProvider {

so I can store this in DB tables like so -

**datastore_provider**
provider_id  |  provider_name

**native_type**
native_type_id  |  name  |  max_precision  |  max_scale  |  provider_id

I think its me, but I cant navigate the Hibernate specs very easily. New to Java, and newer to JPA + Hibernate. Appreciate the guidance!

Upvotes: 1

Views: 89

Answers (1)

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

Code Sample

I think you can use the following -

@Entity
@Table(name = "native_type")
public class NativeType {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "native_type_id")
    private Long id;

    private String name;

    @Column(name = "max_precision")
    private int maxPrecision;

    @Column(name = "max_scale")
    private byte maxScale;

    @ManyToOne
    @JoinColumn(name="provider_id")
    private DataStoreProvider provider; 

    // rest of the class (getter setter etc.)
}

ID Generation

I am assuming you are auto-generating your database ids, hence I marked it with the @GeneratedValue annotation. As for the strategy, I specified AUTO which is only suitable for local experiments. For production-grade application, you'll have to choose between SEQUENCE, IDENTITY, and TABLE.

Relation Mapping

I assumed that you are going to create a DataStoreProvider entity too. In that case, you can specify the relation between them as described above.

Reference

For further reference, you can consult this excellent book.

Upvotes: 1

Related Questions