JPA @ManyToOne不起作用

时间:2017-06-08 14:52:13

标签: jpa eclipselink many-to-one

我无法理解在JPA实体中保存List时我出错的地方。 我有一个超级人物。客户端类扩展了Person。客户端类的Phone实体列表为@OneToMany(Bidirection),代码如下所示。每当客户端实体与该电话列表保持一致时,列表中的所有电话也会被保存。但是,在电话表中没有记录客户端ID。

@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColum(name="type")
public abstract class Person implements Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="PERSON_SEQ")
@SequenceGenerator(name="PERSON_SEQ",sequenceName="PERSON_SEQ", allocationSize=1,initialValue=1000) 
private Long id;

private String name;

public Long getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}}

Client.class

public class Client extends Person实现Serializable {

private static final long serialVersionUID = 1L;


private String foo;

@OneToMany(cascade=CascadeType.ALL,mappedBy="owner")
private List<Phone> phones;

public List<Phone> getPhones() {
    return phones;
}


public void setPhones(List<Phone> phones) {
    this.phones = phones;
}


public String getFoo() {
    return foo;
}


public void setFoo(String foo) {
    this.foo = foo;
}

}

电话课程

公共类Phone实现Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;


@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idPhone;

private String number;

@ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name="id")
private Person owner;

public Long getIdPhone() {
    return idPhone;
}

public void setIdPhone(Long idPhone) {
    this.idPhone = idPhone;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public Person getOwner() {
    return owner;
}

public void setOwner(Person owner) {
    this.owner = owner;
}

}

ClientController类

@Named(value = "clientController")

@ViewScoped 公共类ClientController扩展BaseController实现Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject
private ClientService service;

@Inject
private Client client;


@Inject 
private Employee employee;

@Inject 
private Phone phone;


public void save(ActionEvent event) {
    System.out.println(" Saving in Controller");

    try {

        client = new Client();
        employee = new Employee();
        Phone p1 = new Phone();
        p1.setNumber("99998888");
        Phone p2 = new Phone();
        p2.setNumber("88887777");
        List<Phone> phones = new ArrayList<Phone>();
        phones.add(p1);
        phones.add(p2);
        client.setName("Novembro" );
        client.setPhones(phones);
        employee.setPhones(phones);

        client.setFoo("foo value" );
        employee.setBar("bar value");

        service.saveOrUpdate(client);

        //client = new Client();
        addMessage(FacesMessage.SEVERITY_INFO, "Cliente registrado com sucesso");
    } catch (Exception e) {
        addMessage(FacesMessage.SEVERITY_ERROR, "Tente mais tarde");
        e.printStackTrace();
    }

}

}

客户服务类

public class ClientService implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject
private ClientDAO dao;


public Client saveOrUpdate(Client client) {
    System.out.println(" Saving in Service");


        return dao.save(client);


}

}

DAO保存方法

public T save(T entity) {
    beginTransaction();
    em.persist(entity);
    em.flush();
    commitAndCloseTransaction();
    return entity;
}

我无法理解为什么它没有按预期工作。我的意思是,从拥有手机的客户端保存带有ID的手机实体。

Database Postgresql 9.6 EclipseLink 2.6.0 JPA 2.1

所有课程都有@Entity和@Discrimator注释

@Entity @DiscriminatorValue(值=&#34; C&#34)

@Entity @table(名称=&#34; PHONE&#34)

0 个答案:

没有答案