Hibernate @OneToMany @ManyToOne插入多个表

时间:2014-12-19 11:18:46

标签: java mysql database hibernate hibernate-mapping

CREATE TABLE `Policy` (
`policyId` int(11) NOT NULL AUTO_INCREMENT,
`policyName` varchar(30) NOT NULL,
`roleId` int(11) NOT NULL,
 PRIMARY KEY (`policyId`)
 CONSTRAINT `policy_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `Role` (`roleId`) ON DELETE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Role` (
`roleId` int(11) NOT NULL AUTO_INCREMENT,
`rolename` varchar(30) NOT NULL,
`roleDescription` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`roleId`),
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;

我描述的角色策略表和POJO的1-Many映射如下

@Entity
@Table(name="policy")
public class AWSPolicy implements Serializable{

@Id
@GeneratedValue
@Column(name = "policyId")
private int policyId;

@Column(name = "policyName")
private String policyName;

@ManyToOne
@JoinColumn(name="roleId")
private Role role;

public AWSRole getRole() {
    return role;
}
public void setRole(Role role) {
    this.role = role;
}

public int getPolicyId() {
    return policyId;
}
public void setPolicyId(int policyId) {
    this.policyId = policyId;
}

public String getPolicyName() {
    return policyName;
}
public void setPolicyName(String policyName) {
    this.policyName = policyName;
}

}




@Entity
@Table(name="role")
public class Role implements Serializable{

@Column(name = "rolename")
private String rolename;

@Id
@GeneratedValue
@Column(name = "roleId")
private int roleId;


@Column(name = "roleDescription")
private String roleDescription;

@OneToMany(cascade=CascadeType.PERSIST, mappedBy = "role")
private Set<AWSPolicy> policies;

public Set<AWSPolicy> getPolicies() {
    return policies;
}
public void setPolicies(Set<AWSPolicy> policyList) {
    this.policies = policyList;
}

public int getRoleId() {
    return roleId;
}
public void setRoleId(int roleId) {
    this.roleId = roleId;
}

public String getRolename() {
    return rolename;
}
public void setRolename(String rolename) {
    this.rolename = rolename;
}

public String getRoleDescription() {
    return roleDescription;
}
public void setRoleDescription(String roleDescription) {
    this.roleDescription = roleDescription;
}
}

问题在于,当我尝试插入表时,它只插入Role表而不插入策略表。有人可以帮我弄这个吗。

    Role role = new Role();
    Set<Policy> policyList = new HashSet<Policy>();
    Policy policy = new Policy();
    policy.setPolicyName("uberpolicy");
    policyList.add(policy);

    role.setRolename("uberrole");
    role.setRoleDescription("uberrole");     

    role.setPolicies(policyList);

    Session session = getSessionFactory().getCurrentSession();
        session.save(role);

3 个答案:

答案 0 :(得分:0)

可能这个例子会对你有所帮助 http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/ http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/ 你能否删除cascade = CascadeType.PERSIST并尝试。

你能写出你的查询输出,以便我可以帮助你。

答案 1 :(得分:0)

我尝试了你发布的代码。我看到在ROLE和POLICY表上执行插入查询,但POLICY表的roleId列中的值为null。这是因为您已在策略实例中设置了角色对象。你必须设置一个关系的两端。

以下代码应更新外键值

    Role role = new Role();
    Set<Policy> policyList = new HashSet<Policy>();
    Policy policy = new Policy();
    policy.setPolicyName("uberpolicy");

    policy.setRole(role);      //Set the role object in the policy through a setter.

    policyList.add(policy);

    role.setRolename("uberrole");
    role.setRoleDescription("uberrole");     

    role.setPolicies(policyList);

    Session session = getSessionFactory().getCurrentSession();
    session.save(role);

答案 2 :(得分:0)

以下是修复

@Entity
@Table(name="cas_aws_role")
public class AWSRole implements Serializable{

@Column(name = "casAWSRolename")
private String casAWSRolename;

@Id
@GeneratedValue
@Column(name = "casAWSRoleId")
private int casAWSRoleId;

@Column(name = "casAWSRoleDisplayName")
private String casAWSRoleDisplayName;


@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="casAWSRoleId")
private Set<AWSPolicy> policies = new HashSet<AWSPolicy>();

// method to manage the bidirectional association
public void addToPolicies(AWSPolicy awsPolicy) {
    this.policies.add(awsPolicy);
    awsPolicy.setAWSRole(this);
}

@ManyToOne
@JoinColumn(name="casAWSRolesetId")
private AWSRoleset awsRoleset;

public AWSRoleset getAWSRoleset() {
    return awsRoleset;
}
public void setAWSRoleset(AWSRoleset awsRoleset) {
    this.awsRoleset = awsRoleset;
}


public Set<AWSPolicy> getPolicies() {
    return policies;
}
public void setPolicies(Set<AWSPolicy> policyList) {
    this.policies = policyList;
}

public int getCasAWSRoleId() {
    return casAWSRoleId;
}
public void setCasAWSRoleId(int casAWSRoleId) {
    this.casAWSRoleId = casAWSRoleId;
}

public String getCasAWSRolename() {
    return casAWSRolename;
}
public void setCasAWSRolename(String casAWSRolename) {
    this.casAWSRolename = casAWSRolename;
}


public String getCasAWSRoleDisplayName() {
    return casAWSRoleDisplayName;
}
public void setCasAWSRoleDisplayName(String casAWSRoleDisplayName) {
    this.casAWSRoleDisplayName = casAWSRoleDisplayName;
}

}


@Entity
@Table(name="cas_aws_policy")
public class AWSPolicy implements Serializable{

@Id
@GeneratedValue
@Column(name = "casAWSPolicyId")
private int casAWSPolicyId;

@Column(name = "casAWSPolicyName")
private String casAWSPolicyName;


@ManyToOne
@JoinColumn(name="casAWSRoleId")
private AWSRole awsRole;

public AWSRole getAWSRole() {
    return awsRole;
}
public void setAWSRole(AWSRole awsRole) {
    this.awsRole = awsRole;
}


public int getCasAWSPolicyId() {
    return casAWSPolicyId;
}
public void setCasAWSPolicyId(int casAWSPolicyId) {
    this.casAWSPolicyId = casAWSPolicyId;
}

public String getCasAWSPolicyName() {
    return casAWSPolicyName;
}
public void setCasAWSPolicyName(String casAWSPolicyName) {
    this.casAWSPolicyName = casAWSPolicyName;
}

}