Hibernate 可能与联接表中排序列的多对多关系不更新索引始终为零

Hibernate 可能与联接表中排序列的多对多关系不更新索引始终为零,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我正试图通过联接表中的额外字段保存多对多关系。 我的实体是Custom_事件和Custom_操作以及CustomEventAction “自定义\u事件可以有许多自定义\u操作,自定义\u操作可以有许多自定义\u事件” 问题是CustomEvent、Custom_操作和CustomEventAction正在保存,但排序值始终设置为零 根据我的测试方法,它向CustomAction表和CustomEventAction插入了两个自定义_操作,但排序值始终为零(sql输出的附加图像 这是我的Hib

我正试图通过联接表中的额外字段保存多对多关系。 我的实体是Custom_事件和Custom_操作以及CustomEventAction

“自定义\u事件可以有许多自定义\u操作,自定义\u操作可以有许多自定义\u事件”

问题是CustomEvent、Custom_操作和CustomEventAction正在保存,但排序值始终设置为零

根据我的测试方法,它向CustomAction表和CustomEventAction插入了两个自定义_操作,但排序值始终为零(sql输出的附加图像

这是我的HibernateXML映射

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.apps.cms.customevents.domain">

        <class name="CustomEvent" table="custom_event">
            <cache usage="nonstrict-read-write" />
            <id name="id" type="long" column="event_id" unsaved-value="0">
                <generator class="native" />
            </id>
            <property name="name" column="name" />
            <property name="description" column="description" />
            <property name="onceOnly" column="once_only" type="yes_no"
                not-null="true" />
            <property name="stepCompletionSequential" column="step_completion_sequential"
                type="yes_no" not-null="true" />
            <property name="helpTxtStyle" />
            <many-to-one name="site" class="com.Site"
                column="site_id" not-found="ignore" />
            <property name="createdBy" column="created_by" />
            <property name="created" column="created_date" />
            <property name="lastModifiedBy" column="last_modified_by" />
            <property name="lastModified" column="last_modified_date" />

            <list name="eventActions" table="custom_event_actions" inverse="true" lazy="false" fetch="select" cascade="all">
                <key column="action_id" not-null="true" />
                <index column="sort" />
                <one-to-many  class="com.apps.cms.customevents.domain.CustomEventActions" />
            </list>

            <property name="deleted" column="deleted" type="yes_no" not-null="true" />
        </class>


        <class name="CustomAction" table="custom_action">
            <cache usage="nonstrict-read-write" />
            <id name="id" type="long" column="action_id" unsaved-value="0">
                <generator class="native" />
            </id>
            <property name="name" column="name" />
            <property name="description" column="description" />
            <property name="helpTxtStyle" />
            <many-to-one name="site" class="com.Site"
                column="site_id" not-found="ignore" />
            <property name="customForm" column="custom_form_id" />
            <property name="showStatus" column="show_status" type="yes_no" not-null="true" />
            <property name="showTimeOccurance" column="show_time_occurance"  type="yes_no" not-null="true" />
            <property name="showComments" column="show_comments" type="yes_no" not-null="true" />
            <property name="hasCustomStatus" column="has_custom_status" type="yes_no" not-null="true" />
            <property name="allowReset" column="allow_reset" type="yes_no" not-null="true" />
            <property name="deleted" column="deleted" type="yes_no" not-null="true" />
            <property name="createdBy" column="created_by" />
            <property name="created" column="created_date" />
            <property name="lastModifiedBy" column="last_modified_by" />
            <property name="lastModified" column="last_modified_date" />
        </class>


    <class name="CustomEventActions" table="custom_event_actions">
        <composite-id name="eventActionId" class="com.apps.cms.customevents.domain.CustomEventActionId">
            <key-many-to-one name="customEvent"  class="com.apps.cms.customevents.domain.CustomEvent" column="event_id" />
            <key-many-to-one name="customAction" class="com.apps.cms.customevents.domain.CustomAction" column="action_id" />
        </composite-id>
         <property name="id" type="java.lang.Long" column="id" />
         <property name="required" type="java.lang.Boolean" column="required" />
    </class>


    </hibernate-mapping>


Pojo's are


public class CustomEvent implements Serializable {
    private List<CustomEventActions> eventActions;
    private String name;
    private String description;
    private Site site;
    private CustomFormConstants.HelpTxtDisplayStyle helpTxtStyle;
    private boolean onceOnly;
    private boolean stepCompletionSequential;
    private boolean deleted;
    private long id;
    private Date created;
    private Account createdBy;
    private Date lastModified;
    private Account lastModifiedBy;


public class CustomAction implements Serializable {


    private final static Logger log = LoggerFactory.getLogger(CustomAction.class);
    private CustomForm customForm;
    private boolean showTimeOccurance;
    private Set<CustomActionPlan> customActionPlans;
    private String name;
    private String description;
    private Site site;
    private boolean showStatus;
    private boolean showComments;
    private boolean allowReset;
    private boolean hasCustomStatus;
    private boolean deleted;
    private CustomFormConstants.HelpTxtDisplayStyle helpTxtStyle;
    private long id;
    private Date created;
    private Account createdBy;
    private Date lastModified;
    private Account lastModifiedBy;
}



public class CustomEventActions implements Serializable {

    private CustomEventActionId eventActionId = null;
    private boolean required;

    public CustomEventActions() {
        eventActionId = new CustomEventActionId();
    }
}


public class CustomEventActionId implements Serializable {
    private static final long serialVersionUID = 1L;
    private CustomEvent customEvent;
    private CustomAction customAction;

}


TestingMethod:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response creeateCustomEvent(@Context final HttpServletRequest request, final CustomEvent event) {
        Response.Status status = Response.Status.OK;
        Object jsonResponse = "";
        try {
            Account loggedUserAccount = Account.getAccount(request.getSession());
            if (loggedUserAccount != null) {
                if (event != null) {
                    event.setCreated(new Date());

                    List<CustomEventActions> customActionList = new ArrayList<CustomEventActions>();

                    CustomAction action1 = new CustomAction();
                    action1.setName("action1-" + event.getName());
                    action1.setDescription("description1-" + event.getName());
                    action1.setSite(App.getSite(request));
                    action1.setLastModified(new Date());
                    action1.setCreatedBy(loggedUserAccount);
                    action1.setLastModifiedBy(loggedUserAccount);
                    action1.setCreated(new Date());
                    action1.setSite(App.getSite(request));
                    action1.save();

                    log.info("action1-hashCode = " + action1.hashCode());

                    CustomEventActions customEventActions1 = new CustomEventActions();
                    customEventActions1.getEventActionId().setCustomEvent(event);
                    customEventActions1.getEventActionId().setCustomAction(action1);
                    customEventActions1.setRequired(false);
                    customActionList.add(customEventActions1);
                    log.info("customEventActions1-hashCode = " + customEventActions1.hashCode());

                    CustomAction action2 = new CustomAction();
                    action2.setName("action2-" + event.getName());
                    action2.setDescription("description2-" + event.getName());
                    action2.setSite(App.getSite(request));
                    action2.setLastModified(new Date());
                    action2.setCreatedBy(loggedUserAccount);
                    action2.setLastModifiedBy(loggedUserAccount);
                    action2.setCreated(new Date());
                    action2.setSite(App.getSite(request));
                    action2.save();
                    log.info("action2-hashCode = " + action2.hashCode());
                    CustomEventActions customEventActions2 = new CustomEventActions();
                    customEventActions2.getEventActionId().setCustomEvent(event);
                    customEventActions2.getEventActionId().setCustomAction(action2);
                    customEventActions2.setRequired(true);
                    customActionList.add(customEventActions2);
                    log.info("customEventActions2-hashCode = " + customEventActions2.hashCode());
                    event.setEventActions(customActionList);

                    event.setLastModified(new Date());
                    event.setCreatedBy(loggedUserAccount);
                    event.setLastModifiedBy(loggedUserAccount);
                    event.setCreated(new Date());
                    event.setSite(App.getSite(request));

                    customEventService.saveCustomEvent(event);
                    return Response.ok(event, MediaType.APPLICATION_JSON).build();
                }
            } else {
                status = Response.Status.UNAUTHORIZED;
                jsonResponse = "UNAUTHORIZED";
            }
        } catch (Exception exception) {
            status = Response.Status.INTERNAL_SERVER_ERROR;
            jsonResponse = exception.getLocalizedMessage();
            exception.printStackTrace();
        }
        return Response.status(status).entity(jsonResponse).build();
    }

SQL:
CREATE TABLE `custom_event` (
  `event_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(400)  DEFAULT '',
  `once_only` char(1) DEFAULT 'N',
  `step_completion_sequential` char(1) DEFAULT 'N',
  `helpTxtStyle` char(1) DEFAULT NULL,
  `site_id` bigint(20) NOT NULL DEFAULT '0',
  `created_by` int(11) NOT NULL DEFAULT '0',
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_modified_by` int(11) NOT NULL DEFAULT '0',
  `last_modified_date`  datetime DEFAULT NULL,
  `deleted` char(1) NOT NULL DEFAULT 'N',
  PRIMARY KEY (`event_id`),
  key(site_id)
) DEFAULT CHARSET=utf8;




CREATE TABLE `custom_action` (
  `action_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(400)  DEFAULT '',
   `helpTxtStyle` char(1) DEFAULT NULL,
  `site_id` bigint(20) NOT NULL DEFAULT '0',
 `custom_form_id` int(11),
 `show_status` char(1) NOT NULL DEFAULT 'Y',
 `show_time_occurance` char(1) NOT NULL DEFAULT 'Y',
 `show_comments` char(1) NOT NULL DEFAULT 'Y',
 `has_custom_status` char(1) NOT NULL DEFAULT 'Y',
  `allow_reset` char(1) NOT NULL DEFAULT 'N',
  `deleted` char(1) NOT NULL DEFAULT 'N',
  `created_by` int(11) NOT NULL DEFAULT '0',
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_modified_by` int(11) NOT NULL DEFAULT '0',
  `last_modified_date` datetime DEFAULT NULL,
  PRIMARY KEY (`action_id`),
   key(site_id)
)DEFAULT CHARSET=utf8;


CREATE TABLE `custom_event_actions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_id` int(11) NOT NULL,
  `action_id` int(11) NOT NULL ,
  `sort` int(3) DEFAULT '0',
  `required` char(1) DEFAULT 'N',
   PRIMARY KEY (`id`),
  FOREIGN KEY (event_id) REFERENCES custom_event(event_id),
  FOREIGN KEY (action_id) REFERENCES custom_action(action_id)
)  DEFAULT CHARSET=utf8;

波乔的是
公共类CustomEvent实现可序列化{
私人行动;
私有字符串名称;
私有字符串描述;
私人地盘;
私有CustomFormConstants.HelpTxtDisplayStyle helpTxtStyle;
一次私有布尔;
私有布尔stepCompletionSequential;
删除私有布尔值;
私人长id;
创建私人日期;
创建私人账户;
上次修改的私人日期;
最近修改的私人账户;
公共类CustomAction实现可序列化{
私有最终静态记录器log=LoggerFactory.getLogger(CustomAction.class);
私人定制;
私有布尔showtimeaccurance;
私人制定的行动计划;
私有字符串名称;
私有字符串描述;
私人地盘;
私人身份;
私人评论;
私有布尔AllowSet;
私有布尔状态;
删除私有布尔值;
私有CustomFormConstants.HelpTxtDisplayStyle helpTxtStyle;
私人长id;
创建私人日期;
创建私人账户;
上次修改的私人日期;
最近修改的私人账户;
}
公共类CustomEventActions实现可序列化{
私有CustomEventActionId eventActionId=null;
需要私有布尔值;
公共CustomEventActions(){
eventActionId=新的CustomEventActionId();
}
}
公共类CustomEventActionId实现可序列化{
私有静态最终长serialVersionUID=1L;
私人定制活动;
私人习惯行动;
}
试验方法:
@职位
@使用(MediaType.APPLICATION_JSON)
@产生(MediaType.APPLICATION_JSON)
公共响应CreateCustomEvent(@Context final HttpServletRequest请求,final CustomEvent事件){
Response.Status状态=Response.Status.OK;
对象jsonResponse=“”;
试一试{
Account loggedUserAccount=Account.getAccount(request.getSession());
if(loggedUserAccount!=null){
如果(事件!=null){
event.setCreated(新日期());
List customActionList=新建ArrayList();
CustomAction action1=新的CustomAction();
action1.setName(“action1-”+event.getName());
action1.setDescription(“description1-”+event.getName());
行动1.设置站点(应用程序获取站点(请求));
操作1.setLastModified(新日期());
操作1.设置CreatedBy(loggedUserAccount);
操作1.设置LastModifiedBy(loggedUserAccount);
操作1.设置已创建(新日期());
行动1.设置站点(应用程序获取站点(请求));
操作1.保存();
log.info(“action1 hashCode=“+action1.hashCode());
CustomEventActions customEventActions1=新的CustomEventActions();
customEventActions1.getEventActionId().setCustomEvent(事件);
customEventActions1.getEventActionId().setCustomAction(action1);
customEventActions1.setRequired(false);
customActionList.add(customEventActions1);
log.info(“customEventActions1 hashCode=“+customEventActions1.hashCode());
CustomAction action2=新的CustomAction();
action2.setName(“action2-”+event.getName());
action2.setDescription(“description2-”+event.getName());
行动2.设置站点(应用程序获取站点(请求));
操作2.setLastModified(新日期());
操作2.设置CreatedBy(loggedUserAccount);
操作2.设置最后修改人(loggedUserAccount);
操作2.设置已创建(新日期());
行动2.设置站点(应用程序获取站点(请求));
动作2.保存();
log.info(“action2 hashCode=“+action2.hashCode());
CustomEventActions customEventActions2=新的CustomEventActions();
customEventActions2.getEventActionId().setCustomEvent(事件);
customEventActions2.getEventActionId().setCustomAction(action2);
customEventActions2.setRequired(true);
customActionList.add(customEventActions2);
log.info(“customEventActions2 hashCode=“+customEventActions2.hashCode());
event.setEventActions(customActionList);
event.setLastModified(新日期());
However, there is no inverse="true" for a many-to-one mapping so you need to 
simulate this attribute on a <many-to-one>:

<class name="Bid"
      table="BID">
   ...
   <many-to-one name="item"
column="ITEM_ID"
class="Item"
not-null="true"
insert="false"
update="false"/>
</class>