JPA @EmbeddedId 未生成序列

发布于 2024-10-07 15:29:31 字数 5934 浏览 5 评论 0原文

我有一张表,其中包含一个序列和两个外键的复合主键 我能够保留我的实体类,但它没有根据顺序生成。具有由一个序列和两个外键组成的复合主键的表,maven 中的 hbm2java 给出以下实体

这是主要实体


package aop.web.teacher.rmodels;

// Generated Dec 14, 2010 8:45:32 PM by Hibernate Tools 3.2.2.GA

import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Schoolmaster generated by hbm2java
 */
@Entity
@Table(name = "schoolmaster", schema = "public")
public class Schoolmaster implements java.io.Serializable {

 private SchoolmasterId id;
        ...


 @EmbeddedId
 @AttributeOverrides({
   @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
   @AttributeOverride(name = "districtId", column = @Column(name = "district_id", nullable = false)),
   @AttributeOverride(name = "typeOfSchool", column = @Column(name = "type_of_school", nullable = false)) })
 public SchoolmasterId getId() {
  return this.id;
 }

 public void setId(SchoolmasterId id) {
  this.id = id;
 }

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "type_of_school", nullable = false, insertable = false, updatable = false)
 public AopTeachersTypeMaster getAopTeachersTypeMaster() {
  return this.aopTeachersTypeMaster;
 }

 public void setAopTeachersTypeMaster(
   AopTeachersTypeMaster aopTeachersTypeMaster) {
  this.aopTeachersTypeMaster = aopTeachersTypeMaster;
 }

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "school_nature")
 public AopTeachersSchoolNatureMaster getAopTeachersSchoolNatureMaster() {
  return this.aopTeachersSchoolNatureMaster;
 }

 public void setAopTeachersSchoolNatureMaster(
   AopTeachersSchoolNatureMaster aopTeachersSchoolNatureMaster) {
  this.aopTeachersSchoolNatureMaster = aopTeachersSchoolNatureMaster;
 }

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "district_id", nullable = false, insertable = false, updatable = false)
 public AopTeachersDistrictMaster getAopTeachersDistrictMaster() {
  return this.aopTeachersDistrictMaster;
 }

 public void setAopTeachersDistrictMaster(
   AopTeachersDistrictMaster aopTeachersDistrictMaster) {
  this.aopTeachersDistrictMaster = aopTeachersDistrictMaster;
 }

 @Column(name = "school_name", length = 50)
 public String getSchoolName() {
  return this.schoolName;
 }

 public void setSchoolName(String schoolName) {
  this.schoolName = schoolName;
 }

 @Column(name = "school_address")
 public String getSchoolAddress() {
  return this.schoolAddress;
 }

 public void setSchoolAddress(String schoolAddress) {
  this.schoolAddress = schoolAddress;
 }

 @Column(name = "school_phone_number", length = 12)
 public String getSchoolPhoneNumber() {
  return this.schoolPhoneNumber;
 }

 public void setSchoolPhoneNumber(String schoolPhoneNumber) {
  this.schoolPhoneNumber = schoolPhoneNumber;
 }

 @Temporal(TemporalType.DATE)
 @Column(name = "establishment_date", length = 13)
 public Date getEstablishmentDate() {
  return this.establishmentDate;
 }

 public void setEstablishmentDate(Date establishmentDate) {
  this.establishmentDate = establishmentDate;
 }

 @Column(name = "school_no_of_teachers")
 public Integer getSchoolNoOfTeachers() {
  return this.schoolNoOfTeachers;
 }

 public void setSchoolNoOfTeachers(Integer schoolNoOfTeachers) {
  this.schoolNoOfTeachers = schoolNoOfTeachers;
 }

 @Column(name = "school_no_of_students")
 public Integer getSchoolNoOfStudents() {
  return this.schoolNoOfStudents;
 }

 public void setSchoolNoOfStudents(Integer schoolNoOfStudents) {
  this.schoolNoOfStudents = schoolNoOfStudents;
 }

}


这是嵌入的 PK 类。


/**
 * SchoolmasterId generated by hbm2java
 */
@Embeddable
public class SchoolmasterId  implements java.io.Serializable {


     private long id;
     private long districtId;
     private long typeOfSchool;

    public SchoolmasterId() {
    }

    public SchoolmasterId(long id, long districtId, long typeOfSchool) {
       this.id = id;
       this.districtId = districtId;
       this.typeOfSchool = typeOfSchool;
    }


    @Column(name="id", nullable=false)
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @NaturalId
    @Column(name="district_id", nullable=false)
    public long getDistrictId() {
        return this.districtId;
    }

    public void setDistrictId(long districtId) {
        this.districtId = districtId;
    }
    @NaturalId
    @Column(name="type_of_school", nullable=false)
    public long getTypeOfSchool() {
        return this.typeOfSchool;
    }

    public void setTypeOfSchool(long typeOfSchool) {
        this.typeOfSchool = typeOfSchool;
    }


   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
   if ( (other == null ) ) return false;
   if ( !(other instanceof SchoolmasterId) ) return false;
   SchoolmasterId castOther = ( SchoolmasterId ) other; 

   return (this.getId()==castOther.getId())
 && (this.getDistrictId()==castOther.getDistrictId())
 && (this.getTypeOfSchool()==castOther.getTypeOfSchool());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + (int) this.getId();
         result = 37 * result + (int) this.getDistrictId();
         result = 37 * result + (int) this.getTypeOfSchool();
         return result;
   }   


}


在这里我期望 Id 能够自动生成...... 我只添加了


@NaturalId

并且


@GeneratedValue(strategy=GenerationType.SEQUENCE)

我也尝试过 GenerationType.AUTO 但没有用。 请建议。

I have a table which has composite primary key consisting one sequence and two foreign keys
I am able to persist My entity class but it is not generating according to the sequence. The table which has composite primary key consisting one sequence and two foreign keys, hbm2java in maven gives following entities

Here is the Main Entity


package aop.web.teacher.rmodels;

// Generated Dec 14, 2010 8:45:32 PM by Hibernate Tools 3.2.2.GA

import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Schoolmaster generated by hbm2java
 */
@Entity
@Table(name = "schoolmaster", schema = "public")
public class Schoolmaster implements java.io.Serializable {

 private SchoolmasterId id;
        ...


 @EmbeddedId
 @AttributeOverrides({
   @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
   @AttributeOverride(name = "districtId", column = @Column(name = "district_id", nullable = false)),
   @AttributeOverride(name = "typeOfSchool", column = @Column(name = "type_of_school", nullable = false)) })
 public SchoolmasterId getId() {
  return this.id;
 }

 public void setId(SchoolmasterId id) {
  this.id = id;
 }

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "type_of_school", nullable = false, insertable = false, updatable = false)
 public AopTeachersTypeMaster getAopTeachersTypeMaster() {
  return this.aopTeachersTypeMaster;
 }

 public void setAopTeachersTypeMaster(
   AopTeachersTypeMaster aopTeachersTypeMaster) {
  this.aopTeachersTypeMaster = aopTeachersTypeMaster;
 }

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "school_nature")
 public AopTeachersSchoolNatureMaster getAopTeachersSchoolNatureMaster() {
  return this.aopTeachersSchoolNatureMaster;
 }

 public void setAopTeachersSchoolNatureMaster(
   AopTeachersSchoolNatureMaster aopTeachersSchoolNatureMaster) {
  this.aopTeachersSchoolNatureMaster = aopTeachersSchoolNatureMaster;
 }

 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "district_id", nullable = false, insertable = false, updatable = false)
 public AopTeachersDistrictMaster getAopTeachersDistrictMaster() {
  return this.aopTeachersDistrictMaster;
 }

 public void setAopTeachersDistrictMaster(
   AopTeachersDistrictMaster aopTeachersDistrictMaster) {
  this.aopTeachersDistrictMaster = aopTeachersDistrictMaster;
 }

 @Column(name = "school_name", length = 50)
 public String getSchoolName() {
  return this.schoolName;
 }

 public void setSchoolName(String schoolName) {
  this.schoolName = schoolName;
 }

 @Column(name = "school_address")
 public String getSchoolAddress() {
  return this.schoolAddress;
 }

 public void setSchoolAddress(String schoolAddress) {
  this.schoolAddress = schoolAddress;
 }

 @Column(name = "school_phone_number", length = 12)
 public String getSchoolPhoneNumber() {
  return this.schoolPhoneNumber;
 }

 public void setSchoolPhoneNumber(String schoolPhoneNumber) {
  this.schoolPhoneNumber = schoolPhoneNumber;
 }

 @Temporal(TemporalType.DATE)
 @Column(name = "establishment_date", length = 13)
 public Date getEstablishmentDate() {
  return this.establishmentDate;
 }

 public void setEstablishmentDate(Date establishmentDate) {
  this.establishmentDate = establishmentDate;
 }

 @Column(name = "school_no_of_teachers")
 public Integer getSchoolNoOfTeachers() {
  return this.schoolNoOfTeachers;
 }

 public void setSchoolNoOfTeachers(Integer schoolNoOfTeachers) {
  this.schoolNoOfTeachers = schoolNoOfTeachers;
 }

 @Column(name = "school_no_of_students")
 public Integer getSchoolNoOfStudents() {
  return this.schoolNoOfStudents;
 }

 public void setSchoolNoOfStudents(Integer schoolNoOfStudents) {
  this.schoolNoOfStudents = schoolNoOfStudents;
 }

}


Here is the embedded PK class.


/**
 * SchoolmasterId generated by hbm2java
 */
@Embeddable
public class SchoolmasterId  implements java.io.Serializable {


     private long id;
     private long districtId;
     private long typeOfSchool;

    public SchoolmasterId() {
    }

    public SchoolmasterId(long id, long districtId, long typeOfSchool) {
       this.id = id;
       this.districtId = districtId;
       this.typeOfSchool = typeOfSchool;
    }


    @Column(name="id", nullable=false)
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @NaturalId
    @Column(name="district_id", nullable=false)
    public long getDistrictId() {
        return this.districtId;
    }

    public void setDistrictId(long districtId) {
        this.districtId = districtId;
    }
    @NaturalId
    @Column(name="type_of_school", nullable=false)
    public long getTypeOfSchool() {
        return this.typeOfSchool;
    }

    public void setTypeOfSchool(long typeOfSchool) {
        this.typeOfSchool = typeOfSchool;
    }


   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
   if ( (other == null ) ) return false;
   if ( !(other instanceof SchoolmasterId) ) return false;
   SchoolmasterId castOther = ( SchoolmasterId ) other; 

   return (this.getId()==castOther.getId())
 && (this.getDistrictId()==castOther.getDistrictId())
 && (this.getTypeOfSchool()==castOther.getTypeOfSchool());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + (int) this.getId();
         result = 37 * result + (int) this.getDistrictId();
         result = 37 * result + (int) this.getTypeOfSchool();
         return result;
   }   


}


Here I am expecting the Id to be autogenerated...
I have only added


@NaturalId

and


@GeneratedValue(strategy=GenerationType.SEQUENCE)

I have also tried with GenerationType.AUTO
but did not work.
Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-10-14 15:29:31

此问题有一个解决方法。我也遇到过同样的情况,有 4 个字段作为组合键,其中 1 个需要按序列生成。
我根本没有创建嵌入式类,只有 1 个需要按序列生成的 @Id 字段。其余所有字段值都将是简单列,因为数据库中强制执行引用完整性,而且我正在检查代码中其余 3 个字段的值是否不为空。

如果出现错误,事务将回滚。

There's one workaround for this issue. I've faced the same condition, have 4 fields as composite keys, out of which 1 need to be generated by sequence.
I've not created Embedded class at all, only have 1 @Id field which need to be generated by sequence. Rest all field values will be simple columns, as referential integrity are enforced in DB and also I'm checking values of rest of 3 fields in code to be not null.

In case of error the transaction will rollback.

肤浅与狂妄 2024-10-14 15:29:31

只是想添加我的 2c。这适用于复合主键和单个主键。防止创建序列,而是从表中选择 max + 1。

Identific.java

package my.app.hibernate;

import java.io.Serializable;

public interface Identifiable<T extends Serializable> {
    T getId();
}

CompositeKeyEntity.java

 package my.app.hibernate;

 import java.io.Serializable;

 public interface CompositeKeyEntity<T extends Serializable> extends Identifiable<T> {
 }

SingleKeyEntity.java

package my.app.hibernate;

import java.io.Serializable;

public interface SingleKeyEntity<T extends Serializable> extends Identifiable<T> {
}

AssignedIdentityGenerator.java

package my.app.hibernate;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.internal.CriteriaImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.util.FieldUtils;

public class AssignedIdentityGenerator extends IdentityGenerator {
    private static final String ID_FIELD_NAME = "id";
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());
    private Field sequenceField;
    private String entityClassName;

    @Override
    public Serializable generate(SessionImplementor session, Object obj) {
        @SuppressWarnings("unchecked")
        Identifiable<Serializable> identifiable = (Identifiable<Serializable>)obj;

        entityClassName = obj.getClass().getName();
        Criteria criteria = new CriteriaImpl(entityClassName, session);
        criteria.setReadOnly(true);
        Object toSet = null;

        if (identifiable instanceof CompositeKeyEntity) {
            Serializable id = identifiable.getId();
            if (id != null) {
                String embaddebleClassName = id.getClass().getName();
                buildCriteriaForEmbeddedId(id, embaddebleClassName, criteria);
                toSet = id;
            }
        } else if (obj instanceof SingleKeyEntity) {
            toSet = identifiable;
            sequenceField = FieldUtils.getField(identifiable.getClass(), ID_FIELD_NAME);
            buildCriteriaForSingleId(criteria);
        }

        Number one = castToSequenceNumberType(1L);
        Number value = (Number) criteria.uniqueResult();

        if(value != null) {
            value = castToSequenceNumberType(value.longValue() + one.longValue());

            setFieldValue(sequenceField, value, toSet);
        } else {
            value = one;
            setFieldValue(sequenceField, value, toSet);
        }

        return identifiable.getId();
    }

    private void buildCriteriaForSingleId(Criteria criteria) {
        criteria.setProjection(Projections.max(ID_FIELD_NAME).as("seq"));
    }

    private void buildCriteriaForEmbeddedId(Serializable id, String embaddebleClassName, Criteria criteria) {
        List<Field> fields = Arrays.asList(id.getClass().getDeclaredFields());

        class Utils {
            Field field;
            boolean numberFound = false;
        }
        final Utils utils = new Utils();

        for (Field field : fields) {
            if ("serialVersionUID".equals(field.getName()) || "$jacocoData".equals(field.getName())) {
                continue;
            }

            if (Number.class.isAssignableFrom(field.getType())) {
                if (utils.numberFound) {
                    throw new IllegalArgumentException(
                            embaddebleClassName + " has more then one sequence field: " + field.getName() + ", "
                                    + utils.field.getName() + ",...");
                }

                utils.numberFound = true;
                utils.field = field;
                sequenceField = field;

                criteria.setProjection(Projections.max(ID_FIELD_NAME + "." + sequenceField.getName()).as("seq"));
            } else {
                criteria.add(Restrictions.eq(ID_FIELD_NAME + "." + field.getName(), getFieldValue(field, id)));
            }
        }
    }

    private Number castToSequenceNumberType(Number n) {
        return (Number) sequenceField.getType().cast(n);
    }

    private void setFieldValue(Field field, Object value, Object to) {
        try {
            field.setAccessible(true);
            field.set(to, value);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    private Object getFieldValue(Field field, Object from) {
        try {
            field.setAccessible(true);
            return field.get(from);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }

        return null;
    }
}

Customer.java

package my.app.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

import my.app.hibernate.SingleKeyEntity;

@Entity(name = "whatever_entity_name")
@GenericGenerator(name = "WHATEVER_NAMED_GENERATOR", strategy = "my.app.hibernate.AssignedIdentityGenerator")
public class Customer implements SingleKeyEntity<Long> {

    @Id
    @GeneratedValue(generator = "WHATEVER_NAMED_GENERATOR")
    private Long id;
    @Column(nullable = false)
    private String name;
}

CustomerItemsId.java(在 SingleKeyEntity 示例之后省略了 Item.java)

package my.app.entities;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class CustomerItemsId implements Serializable {
    private static final long serialVersionUID = 1L; //generate one

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
    @ManyToOne
    @JoinColumn(name = "item_id")
    private Item item;
    private Long seq; //name as you wish
}

CustomerItems.java

package my.app.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

import my.app.hibernate.CompositeKeyEntity;

@Entity(name = "whatever_entity_name")
@GenericGenerator(name = "WHATEVER_NAMED_GENERATOR", strategy = "my.app.hibernate.AssignedIdentityGenerator")
public class CustomerItems implements CompositeKeyEntity<CustomerItemsId> {

    @GeneratedValue(generator = "WHATEVER_NAMED_GENERATOR")
    private CustomerItems id;
    @Column(nullable = false)
    private String randomColumn1;
    @Column(nullable = false)
    private String randomColumn2;
}

Just wanna to add my 2c. This works with composite and single primary keys. Prevent for creating sequences, instead, select max + 1 from the table.

Identifiable.java

package my.app.hibernate;

import java.io.Serializable;

public interface Identifiable<T extends Serializable> {
    T getId();
}

CompositeKeyEntity.java

 package my.app.hibernate;

 import java.io.Serializable;

 public interface CompositeKeyEntity<T extends Serializable> extends Identifiable<T> {
 }

SingleKeyEntity.java

package my.app.hibernate;

import java.io.Serializable;

public interface SingleKeyEntity<T extends Serializable> extends Identifiable<T> {
}

AssignedIdentityGenerator.java

package my.app.hibernate;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.internal.CriteriaImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.util.FieldUtils;

public class AssignedIdentityGenerator extends IdentityGenerator {
    private static final String ID_FIELD_NAME = "id";
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());
    private Field sequenceField;
    private String entityClassName;

    @Override
    public Serializable generate(SessionImplementor session, Object obj) {
        @SuppressWarnings("unchecked")
        Identifiable<Serializable> identifiable = (Identifiable<Serializable>)obj;

        entityClassName = obj.getClass().getName();
        Criteria criteria = new CriteriaImpl(entityClassName, session);
        criteria.setReadOnly(true);
        Object toSet = null;

        if (identifiable instanceof CompositeKeyEntity) {
            Serializable id = identifiable.getId();
            if (id != null) {
                String embaddebleClassName = id.getClass().getName();
                buildCriteriaForEmbeddedId(id, embaddebleClassName, criteria);
                toSet = id;
            }
        } else if (obj instanceof SingleKeyEntity) {
            toSet = identifiable;
            sequenceField = FieldUtils.getField(identifiable.getClass(), ID_FIELD_NAME);
            buildCriteriaForSingleId(criteria);
        }

        Number one = castToSequenceNumberType(1L);
        Number value = (Number) criteria.uniqueResult();

        if(value != null) {
            value = castToSequenceNumberType(value.longValue() + one.longValue());

            setFieldValue(sequenceField, value, toSet);
        } else {
            value = one;
            setFieldValue(sequenceField, value, toSet);
        }

        return identifiable.getId();
    }

    private void buildCriteriaForSingleId(Criteria criteria) {
        criteria.setProjection(Projections.max(ID_FIELD_NAME).as("seq"));
    }

    private void buildCriteriaForEmbeddedId(Serializable id, String embaddebleClassName, Criteria criteria) {
        List<Field> fields = Arrays.asList(id.getClass().getDeclaredFields());

        class Utils {
            Field field;
            boolean numberFound = false;
        }
        final Utils utils = new Utils();

        for (Field field : fields) {
            if ("serialVersionUID".equals(field.getName()) || "$jacocoData".equals(field.getName())) {
                continue;
            }

            if (Number.class.isAssignableFrom(field.getType())) {
                if (utils.numberFound) {
                    throw new IllegalArgumentException(
                            embaddebleClassName + " has more then one sequence field: " + field.getName() + ", "
                                    + utils.field.getName() + ",...");
                }

                utils.numberFound = true;
                utils.field = field;
                sequenceField = field;

                criteria.setProjection(Projections.max(ID_FIELD_NAME + "." + sequenceField.getName()).as("seq"));
            } else {
                criteria.add(Restrictions.eq(ID_FIELD_NAME + "." + field.getName(), getFieldValue(field, id)));
            }
        }
    }

    private Number castToSequenceNumberType(Number n) {
        return (Number) sequenceField.getType().cast(n);
    }

    private void setFieldValue(Field field, Object value, Object to) {
        try {
            field.setAccessible(true);
            field.set(to, value);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    private Object getFieldValue(Field field, Object from) {
        try {
            field.setAccessible(true);
            return field.get(from);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            LOG.error(e.getMessage(), e);
        }

        return null;
    }
}

Customer.java

package my.app.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

import my.app.hibernate.SingleKeyEntity;

@Entity(name = "whatever_entity_name")
@GenericGenerator(name = "WHATEVER_NAMED_GENERATOR", strategy = "my.app.hibernate.AssignedIdentityGenerator")
public class Customer implements SingleKeyEntity<Long> {

    @Id
    @GeneratedValue(generator = "WHATEVER_NAMED_GENERATOR")
    private Long id;
    @Column(nullable = false)
    private String name;
}

CustomerItemsId.java (Item.java ommited as it follows SingleKeyEntity example)

package my.app.entities;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class CustomerItemsId implements Serializable {
    private static final long serialVersionUID = 1L; //generate one

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
    @ManyToOne
    @JoinColumn(name = "item_id")
    private Item item;
    private Long seq; //name as you wish
}

CustomerItems.java

package my.app.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;

import my.app.hibernate.CompositeKeyEntity;

@Entity(name = "whatever_entity_name")
@GenericGenerator(name = "WHATEVER_NAMED_GENERATOR", strategy = "my.app.hibernate.AssignedIdentityGenerator")
public class CustomerItems implements CompositeKeyEntity<CustomerItemsId> {

    @GeneratedValue(generator = "WHATEVER_NAMED_GENERATOR")
    private CustomerItems id;
    @Column(nullable = false)
    private String randomColumn1;
    @Column(nullable = false)
    private String randomColumn2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文