使用jpa,jboss7.1插入数据库。 @OneToMany注释

时间:2012-05-07 09:41:56

标签: java hibernate jpa jpa-2.0

我正在使用Jboss7.1和jpa,ejb 我想将数据 - 与OneToMany关系 - 插入到我的mysql数据库中。

我有两个实体和人。我想在我的数据库中保存一个人并为他关联voiture。问题是当我测试我的代码(测试)时,我发现我的数据库中添加了一个新的personne,并且表voiture中没有添加voiture

你能帮助我吗?

代码:

实体人物

package com.domain;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;


/**
 * The persistent class for the personne database table.
 * 
 */
@Entity
public class Personne implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idpersonne;

private String nom;

//bi-directional many-to-one association to Voiture
@OneToMany(mappedBy="personne")
private Set<Voiture> voitures;

public Personne() {
}

public Personne(String nom) {
    super();
    this.nom = nom;
}

public int getIdpersonne() {
    return this.idpersonne;
}

public void setIdpersonne(int idpersonne) {
    this.idpersonne = idpersonne;
}

public String getNom() {
    return this.nom;
}

public void setNom(String nom) {
    this.nom = nom;
}

public Set<Voiture> getVoitures() {
    return this.voitures;
}

public void setVoitures(Set<Voiture> voitures) {
    this.voitures = voitures;
}

} 

实体声明

package com.domain;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the voiture database table.
 * 
*/
@Entity
public class Voiture implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int idvoiture;

private String type;

//bi-directional many-to-one association to Personne
@ManyToOne
private Personne personne;

public Voiture() {
}

public Voiture(String type) {
    super();
    this.type = type;
}

public int getIdvoiture() {
    return this.idvoiture;
}

public void setIdvoiture(int idvoiture) {
    this.idvoiture = idvoiture;
}

public String getType() {
    return this.type;
}

public void setType(String type) {
    this.type = type;
}

public Personne getPersonne() {
    return this.personne;
}

public void setPersonne(Personne personne) {
    this.personne = personne;
}

 }

这是界面

 package com.DAO;

import javax.ejb.Remote;

import com.domain.Personne;

@Remote
public interface PersonneDAO {

public void save(Personne personne);
public String sayhello();
}

实施      包com.DAO.Impl;

 import javax.ejb.Stateless;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;

 import com.DAO.VoitureDAO;
 import com.domain.Voiture;
 @Stateless
 public class VoitureDAOImpl implements VoitureDAO {
@PersistenceContext(name = "JPADD")
EntityManager em;

@Override
public void save(Voiture voiture) {
    em.persist(voiture);
}
}

实施     包com.DAO.Impl;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.DAO.PersonneDAO;
import com.domain.Personne;

@Stateless
public class PersonneDAOImpl implements PersonneDAO {
@PersistenceContext(name = "JPADD")
EntityManager em;

@Override
public String sayhello() {
    // TODO Auto-generated method stub
    return "helllllllllllllllllo";
}

@Override
public void save(Personne personne) {
    em.persist(personne);
}
}

这是测试

package test;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.DAO.PersonneDAO;
import com.domain.Personne;
import com.domain.Voiture;

public class Test {

/**
 * @param args
 */

public static void main(String[] args) {

    Context intialcontext;
    Properties properties = new Properties();
    properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    try {
        intialcontext = new InitialContext(properties);
        PersonneDAO dao = (PersonneDAO) intialcontext
                .lookup("ejb:/projetweb/PersonneDAOImpl!com.DAO.PersonneDAO");
        // /----------------------------objet voiture-------------
        Voiture voiture = new Voiture("216");
        Set<Voiture> voitures = new HashSet<Voiture>();
        voitures.add(voiture);
        // -------------------------------------------------------
        Personne personne = new Personne("slatnia");
        personne.setVoitures(voitures);
        dao.save(personne);
    } catch (NamingException e) {
        e.printStackTrace();

    }
}

}

这是我的jboss-ejb-client.properties

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost   
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

2 个答案:

答案 0 :(得分:0)

尝试将以下属性添加到@OneToMany注释

@OneToMany(cascade=CascadeType.ALL)

答案 1 :(得分:0)

您应该在cascade = CascadeType.PERSIST

中添加@OneToMany

<强> CascadeType.PERSIST

  

当持久化实体时,也会持有此实体   领域。我们建议自由应用这种级联规则,因为如果   EntityManager找到一个引用新实体的字段   flush,并且该字段不使用CascadeType.PERSIST,这是一个错误。

示例:

@OneToMany(cascade = CascadeType.PERSIST)
private Set<Voiture> voitures;

CascadeType的Javadoc和here的其他文档。

相关问题