添加一个新实体--One-To-Many关系

时间:2014-12-16 10:32:02

标签: java java-ee

我定义了以下实体:BaseEntitymagasinarticle

 @Entity(name = "magasin")
@Table(name = "magasin")
public class Magasin extends BaseEntity {

    private static final long serialVersionUID = 1L;


    @Basic
    @Size(min=5, max=100, message="The name must be between {min} and {max} characters")
    private String name;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="magasin")
    @Valid
    private Set<Article> article;

    public String getName() {
        return name;
    }

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

    public Set<Article> getArticle() {
        return article;
    }

    public void setArticle(Set<Article> article) {
        this.article = article;
    }
}

@Entity(name="article")
@Table(name="article")
public class Article extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    private Magasin magasin;

    @Basic
    @Size(min=5, max=100, message="The name must be between {min} and {max} characters")
    private String name;
    @Basic
    private float price;

    public Magasin getMagasin() {
        return magasin;
    }

    public void setMagasin(Magasin magasin) {
        this.magasin = magasin;
    }

    public String getName() {
        return name;
    }

    public void setName(String nom) {
        this.name = nom;
    }
    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

}

 @MappedSuperclass
 public class BaseEntity {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

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

public Integer getId() {
    return id;
}

public boolean isNew() {
    return (this.id == null);
}

我尝试添加一个它没有工作的新实体位。没有插入新实体。没有错误。如何修改它以便为文章添加新实体?

GenericDao的功能

public T add(T entity) {
    System.out.println("start");
    getHibernateTemplate().save(entity);
    System.out.println("end");
    return entity;
}

Flex Main

private function submitData(event:Event):void {

            var article_ajout=new Article();
            article_ajout.name=pop1.art_name.text;
            article_ajout.price=parseFloat(pop1.art_prix.text);
            article_ajout.magasin=Magasin(iMagasin.selectedItem);
            Alert.show("test");
            articleService.add(article_ajout);  
            removeMe(event);
        }

0 个答案:

没有答案