MySQL数据库与Wildfly 10,过滤器和休眠

时间:2016-08-17 09:13:38

标签: hibernate java-ee jboss ejb wildfly

我使用Wildfly 10应用程序服务器,需要创建一个包含REST部分和ejb部分的应用程序。在ejb中是persistence.xml来访问数据源。在战争中,EJB被注入。除此之外,我有一个客户端使用JNDI连接到Bean。

Bean:

@Stateless
@Remote(SampleBeanRemote.class) 
public class  SampleBeanRemoteImpl implements SampleBeanRemote  
{
  @PersistenceContext(unitName="workingdb")
  private EntityManager em;

  //methods to access via JNDI (for example em.persist(), em.find, etc.)
}

我需要在ejb POJO(OneToMany)上使用过滤器,所以我认为我需要Hibernate才能执行此操作。我现在已经阅读了很多关于entityManagers,会话,工厂,BMT,CMT的内容,我现在还不知道该怎么做了。如何正确设置?

编辑:
一对多:

@Entity
@Table(name = "arbeitsgaenge")
@FilterDef(name="userFilter", parameters={
        @ParamDef( name="akuser", type="integer")
})
public class Arbeitsgang implements java.io.Serializable
{
    /*
     * FIELDS
     */

    private static final long serialVersionUID = -3812458124424219088L;

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

    @Column(nullable = false)
    private int sollZeit;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String modul;

    @Enumerated(EnumType.STRING)
    private Timestamp.Status status;

    @ManyToOne
    @JoinColumn(name="id_auftrag", insertable = true, updatable = false, nullable = false)
    private Auftrag auftrag;

    @OneToMany(cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @Filter(name = "userFilter", condition="user = :akuser")
    @JoinColumn(name="id_arbeitsgang")
    private List<Timestamp> timestamp = new ArrayList<Timestamp>();

    @OneToMany(cascade={CascadeType.ALL})
    @JoinColumn(name="id_arbeitsgang_ps")
    private List<Progstat> progstat = new ArrayList<Progstat>();

    /*
     * CONST
     */

    public Arbeitsgang(int sollZeit, String name, String modul, Timestamp.Status status, Auftrag auftrag) 
    {
        this.sollZeit = sollZeit;
        this.name = name;
        this.status = status;
        this.auftrag = auftrag;
        this.setModul(modul);
    }

    public Arbeitsgang(int sollZeit, String name, String modul, Auftrag auftrag) 
    {
        this(sollZeit, name, modul, Timestamp.Status.NA, auftrag);
    }

    public Arbeitsgang(int sollZeit, String name, String modul) 
    {
        this(sollZeit, name, modul, Timestamp.Status.NA, null);
    }

    public Arbeitsgang()
    {   }

    /*
     * GETTERS
     */

     .....

    /*
     * SETTERS
     */

     .....

    /*
     * GENERAL
     */

    public void addTimestamp(Timestamp timestamp)
    {
        this.timestamp.add(timestamp);
    }
}

多对一:

@Entity
@Table(name = "timestamp")
public class Timestamp implements java.io.Serializable
{
    /*
     * FIELDS
     */

    private static final long serialVersionUID = 6478056732010632092L;

    public static enum Status { 
        NA, STARTED, PAUSED, RESUMED, FINISHED
    }

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

    @Column(nullable = false)
    private long zeit;
    private long overwrite;

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private Status status;

    @ManyToOne
    @JoinColumn(name="id_arbeitsgang", insertable = true, updatable = false, nullable = false)
    private Arbeitsgang arbeitsgang;

    @ManyToOne
    @JoinColumn(name="id_user", insertable = true, updatable = false, nullable = false)
    private User user;

    /*
     * CONST
     */

    public Timestamp(long zeit, long overwrite, Status status, Arbeitsgang arbeitsgang, User user) 
    {
        this.zeit = zeit;
        this.overwrite = overwrite;
        this.status = status;
        this.arbeitsgang = arbeitsgang;
        this.user = user;
    }

    public Timestamp()
    {   }

    /*
     * GETTERS
     */

     ....

    /*
     * SETTERS
     */

     ....
}

所以基本上,一个“Auftrag”有很多“Arbeitsgaenge”,它有很多“时间戳”,我想要所有的“Arbeitsgaenge”,但只有“Timestamps”才有一定的用户ID。

Class diagramm

0 个答案:

没有答案