当我尝试插入我的通知类型对象时,子实体也尝试插入
When i try to insert my notification type object then child entity also tries to insert
我有一个 notificationTypes table,权限 table。这些是在桥 table notification_types_permission 的帮助下链接起来的。
当所有实体都是新的时,它工作正常。
我的问题是,当我尝试使用现有权限插入新的 notificationTypes 时,那个时间权限也会尝试插入一个新的。
我的 table 和实体结构如下。
CREATE TABLE `notification_types` (
`Id` BIGINT(16) NOT NULL AUTO_INCREMENT,
`TypeName` VARCHAR(80) NOT NULL,
`InsertedDttm` DATETIME NULL,
`InsertedBy` BIGINT(16) NULL,
`UpdatedDttm` DATETIME NULL,
`UpdatedBy` BIGINT(16) NULL,
PRIMARY KEY (`Id`));
CREATE TABLE `notification_types_permission` (
`Id` BIGINT(16) NOT NULL AUTO_INCREMENT,
`NotificationTypes_ID` BIGINT(16) NOT NULL,
`permissions_ID` int(11) NOT NULL,
PRIMARY KEY (`Id`), FOREIGN KEY (NotificationTypes_ID)
REFERENCES `frontoffice`.notification_types(id), FOREIGN KEY (permission_Id)
REFERENCES `frontoffice`.`permission`(id));
public class NotificationTypes {
@Id
private Long id;
private String typeName;
@Temporal(TemporalType.TIMESTAMP)
private Date insertedDttm;
private Long insertedBy;
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDttm;
private Long updatedBy;
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private List<RLPermissions> permissions;
}
首先获取新权限:
Permission p = permissionDao.getPermissionByName("SuperAdmin");
然后像这样添加:
notificationType.notificationTypesPermission.getPermissions().add(p)
你已经设置了 cascade=CascadeType.PERSIST 这将更新关系的多边。对 NotificationType 的任何更改都会更新相应的子实体
CascadeType.PERSIST :表示 save() 或 persist() 操作级联到相关实体。
@OneToMany( fetch = FetchType.EAGER)
private List<RLPermissions> permissions;
如果不需要对权限列表进行删除、更新和插入操作,请避免从该关系中级联属性。
我有一个 notificationTypes table,权限 table。这些是在桥 table notification_types_permission 的帮助下链接起来的。 当所有实体都是新的时,它工作正常。 我的问题是,当我尝试使用现有权限插入新的 notificationTypes 时,那个时间权限也会尝试插入一个新的。 我的 table 和实体结构如下。
CREATE TABLE `notification_types` (
`Id` BIGINT(16) NOT NULL AUTO_INCREMENT,
`TypeName` VARCHAR(80) NOT NULL,
`InsertedDttm` DATETIME NULL,
`InsertedBy` BIGINT(16) NULL,
`UpdatedDttm` DATETIME NULL,
`UpdatedBy` BIGINT(16) NULL,
PRIMARY KEY (`Id`));
CREATE TABLE `notification_types_permission` (
`Id` BIGINT(16) NOT NULL AUTO_INCREMENT,
`NotificationTypes_ID` BIGINT(16) NOT NULL,
`permissions_ID` int(11) NOT NULL,
PRIMARY KEY (`Id`), FOREIGN KEY (NotificationTypes_ID)
REFERENCES `frontoffice`.notification_types(id), FOREIGN KEY (permission_Id)
REFERENCES `frontoffice`.`permission`(id));
public class NotificationTypes {
@Id
private Long id;
private String typeName;
@Temporal(TemporalType.TIMESTAMP)
private Date insertedDttm;
private Long insertedBy;
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDttm;
private Long updatedBy;
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private List<RLPermissions> permissions;
}
首先获取新权限:
Permission p = permissionDao.getPermissionByName("SuperAdmin");
然后像这样添加:
notificationType.notificationTypesPermission.getPermissions().add(p)
你已经设置了 cascade=CascadeType.PERSIST 这将更新关系的多边。对 NotificationType 的任何更改都会更新相应的子实体
CascadeType.PERSIST :表示 save() 或 persist() 操作级联到相关实体。
@OneToMany( fetch = FetchType.EAGER)
private List<RLPermissions> permissions;
如果不需要对权限列表进行删除、更新和插入操作,请避免从该关系中级联属性。