2012-05-07 25 views
0

我正在使用Jboss7.1和jpa,ejb 我想插入数据 - 与OneToMany关系 - 到我的MySQL数据库。使用jpa,jboss7.1插入数据库。 @OneToMany注释

我有两个实体personne和voiture。我想将一个人保存在我的数据库中,并将voiture与他联系起来。问题是,当我测试代码(测试),我发现有一个新的personne添加到我的数据库并没有voiture在表voiture

请你能帮助我补充道。

代码:

实体personne

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; 
} 

} 

实体VOITURE

​​

这是接口

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); 
} 
} 

执行 package 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 
+0

请问您是否可以添加您的错误情况? – UVM

+0

@UNNI有没有在我的控制台产生的错误仅仅存在一个警告,警告:与头接收到不支持的消息为0xffffffff – wadii

回答

0

尝试添加下列属性的@OneToMany批注

@OneToMany(cascade=CascadeType.ALL) 
+0

@UNNI 有没有在我的控制台产生的错误,只有一个警告 警告:与头为0xffffffff收到不受支持的消息 – wadii

0

您应该添加cascade = CascadeType.PERSIST@OneToMany

CascadeType.PERSIST

当持续的实体,还坚持在这个领域 举行的实体。我们建议这个级联规则的自由应用,因为如果 EntityManager找到在 刷新期间引用新实体的字段,并且该字段不使用CascadeType.PERSIST,则它是一个错误。

例如:

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

Javadoc中CascadeType和其他文档在here

+0

我尝试 @OneToMany(级联= CascadeType.PERSIST) 私人设置 voitures; 但它不工作 – wadii