Hibernate JPA,将数据添加到多对多表中的问题

时间:2017-05-16 05:36:04

标签: java hibernate jpa

我正在创建一个数据库,并且无法将数据添加到将运营商连接到他们运营的路由的多对多表中。所有其他数据都添加正常,我完全不知道如何解决这个问题。

这是控制台返回的摘录,它显示操作表,即我无法添加的操作表,具有不同的消息

new SparkConf(true)
        .set("spark.cassandra.connection.host", "10.3.2.1")  
        .set("spark.cassandra.auth.username","cassandra")
        .set("spark.cassandra.auth.password",cassandra"))
        .set("spark.master", "spark://1.1.2.2:7077") // Can I give multiple Urls here?
        .set("spark.app.name","Sample App");

这是我的一些代码,遗漏了get和set方法,因为它们没问题:

路线

Hibernate: 
alter table operates 
    add index FKE22A276FF12BFD7F (number), 
    add constraint FKE22A276FF12BFD7F 
    foreign key (number) 
    references route (number)
Hibernate: 
alter table operates 
    add index FKE22A276FC0CF67CB (name), 
    add constraint FKE22A276FC0CF67CB 
    foreign key (name) 
    references operator (name)
Hibernate: 
insert 
into
    bus_stop
    (description, id) 
values
    (?, ?)
Hibernate: 
insert 
into
    bus_stop
    (description, id) 
values
    (?, ?)

操作

@Entity
@NamedQueries({
    @NamedQuery(name = Route.SELECT_ALL, query = "select r from Route r 
order by r.number asc")
})
@Table(name = "route")
public class Route {
    @Id  @Column(name="number")
    private String number;

    private int frequency;
    private String start;
private String destination;

@ManyToMany(
        cascade = {CascadeType.PERSIST, CascadeType.MERGE},
        mappedBy = "routes",
        targetEntity = Operator.class)
private Set<Operator> operators = new HashSet<Operator>();

和各自的插入语句

@Entity
@NamedQueries({
    @NamedQuery(name = Operator.SELECT_ALL, query = "select o from Operator 
 o order by o.name asc")
 })
 @Table(name = "operator")
 public class Operator {

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

private String street;
private String town;
private String postcode;
private String email;
private String phone;

@ManyToMany(
        targetEntity=Route.class,
        cascade={CascadeType.PERSIST, CascadeType.MERGE}
    )
    @JoinTable(
        name="operates",
        joinColumns=@JoinColumn(name="name"),
        inverseJoinColumns=@JoinColumn(name="number")
    )
private Set<Route> routes;

额外评论

    public static Session insert(final Map<String, String> row, Session session) {
    Operator o = new Operator();
    o.setRoutes(new HashSet<Route>());

    for (String key : row.keySet()) {
        if(key.equals("name"))
            o.setName(row.get(key));
        else if(key.equals("street"))
            o.setStreet(row.get(key));
        else if(key.equals("town"))
            o.setTown(row.get(key));
        else if(key.equals("postcode"))
            o.setPostcode(row.get(key));
        else if(key.equals("email"))
            o.setEmail(row.get(key));
        else if(key.equals("phone"))
            o.setPhone(row.get(key));
    }
    session.save(o);

    return session; 

public static Session insert(final Map<String, String> row, final Session session) {

        Route r = new Route();
        r.setOperators(new HashSet<Operator>());

        for (String key : row.keySet()) {
            if(key.equals("number"))
                r.setNumber(row.get(key));
            else if(key.equals("frequency"))
                r.setFrequency(Integer.parseInt(row.get(key)));
            else if(key.equals("start"))
                r.setStart(row.get(key));
            else if(key.equals("destination"))
                    r.setDestination(row.get(key));
            else if(key.equals("operators")){
                Set<String> operatorStrings = new HashSet<String>(Arrays.asList(row.get(key).split("|")));

                Operator o = new Operator();
                for(String s : operatorStrings){
                    o.setName(s);
                    r.getOperators().add(o);
                }
            }
        }

        session.save(r);

        return session;
    }

1 个答案:

答案 0 :(得分:0)

添加Oerator或Route时,请确保Route位于Operators.routes集合中,且运算符位于Route.operators

在路线中:

public void addOperator(Operator o){
operators.add(o);
o.addRoute(this);
}

在运营商中:

public void addRoute(Route r){
routes.add(r);
r.addOperator(this);
}

移除路线或操作员时也一样。这可以确保多对多关系的双方同步。