ejb-3.0

How to inject persistence context to different data source programmatically

二次信任 提交于 2019-11-26 16:29:32
问题 In standard EJB 3, when injecting entity manager, persistence unit (which refers to datasource) is hardcoded into annotation: (or alternatively xml file) @PersistenceContext(unitName = "myunit") private EntityManager entityManager; Is there a way to use an entity manager but to select data source by name at runtime? 回答1: Using EclipseLink, You can set a DataSource configured in your app server. import org.eclipse.persistence.config.PersistenceUnitProperties; ... .... Map props = new HashMap()

“detached entity passed to persist error” with JPA/EJB code

陌路散爱 提交于 2019-11-26 12:57:00
I am trying to run this basic JPA/EJB code: public static void main(String[] args){ UserBean user = new UserBean(); user.setId(1); user.setUserName("name1"); user.setPassword("passwd1"); em.persist(user); } I get this error: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.JPA.Database Any ideas? I search on the internet and the reason I found was: This was caused by how you created the objects, i.e. If you set the ID property explicitly. Removing ID assignment fixed it. But I didn't get it, what

Difference between a “jta-datasource” and a “ resource-local ” datasource?

百般思念 提交于 2019-11-26 11:48:29
问题 The terms \"jta-datasource\" and \"resource-local datasource\" are a little vague to me. I\'m putting down what I am understanding ( or assuming ) and I\'d like you to say where I\'m right / wrong. The same database can be referred to as a jta-datasource or as a resource local datasource If mentioned as jta-datasource, then the beans / other classes can use JTA. Hence, UserTransaction interface Cannot use CMT / BMT if the datasource is resource local If mentioned as resource local datasource,

Confusion: @NotNull vs @Column(nullable = false)

让人想犯罪 __ 提交于 2019-11-26 11:34:42
When they appear on a field/getter of an @Entity , what is the difference between them? (I persist the Entity through Hibernate ). What framework and/or specification each one of them belongs to? @NotNull is located within javax.validation.constraints . In the javax.validation.constraints.NotNull javadoc it says The annotated element must not be null but it does not speak of the element's representation in the database, so why would I add the constraint nullable=false to the column? Ryan Stewart @NotNull is a JSR 303 Bean Validation annotation. It has nothing to do with database constraints

Batch inserts using JPA EntityManager

白昼怎懂夜的黑 提交于 2019-11-26 11:01:24
问题 Is there a way where we can use batch inserts using JPA EntityManager. I know there is no direct way to achieve this but there must be some way to achieve this mechanism. Actually, for every insert operation it\'s taking me 300ms which I want to reduce using batch inserts instead of single inserts. Here\'s code I am using currently executing for single inserts @PersistenceContext(unitName = \"testing\") EntityManager eM; Query querys = this.eM.createNativeQuery(insertQuery); for (String s :

Where to use EJB 3.1 and CDI?

微笑、不失礼 提交于 2019-11-26 08:38:59
问题 I am making a Java EE based product in which I\'m using GlassFish 3 and EJB 3.1. My application has session beans, a scheduler and uses web services. I recently came to know about Apache TomEE, which supports Contexts and Dependency Injection (CDI). The GlassFish container also supports CDI. Can I replace session beans where I do not require any feature that CDI also doesn\'t already provides? And if then, what are the benefits I can get? 回答1: Yes, you can freely mix both CDI and EJB and

Eager / auto loading of EJB / load EJB on startup (on JBoss)

自闭症网瘾萝莉.ら 提交于 2019-11-26 08:16:23
问题 EJBs seem to be loaded lazily - whenever accessed. However, I want to initialize them eagerly - i.e. whenever the container starts-up. How is this achieved (in JBoss in particular) This topic gives some hints, but isn\'t quite satisfactory. 回答1: As of EJB 3.1, singleton beans can be notified of module start and stop: @Singleton @Startup public class StartupBean { @PostConstruct private void postConstruct() { /* ... */ } @PreDestroy private void preDestroy() { /* ... */ } } Prior to EJB 3.1,

java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities

眉间皱痕 提交于 2019-11-26 07:41:07
问题 I have 3 entities with ManyToMany relationships: Role Entity: @Entity public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer roleID; private String roleName; private String description; @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER) @JoinTable(name = \"role_permission\", joinColumns = {@JoinColumn(name = \"role_id\")}, inverseJoinColumns = {@JoinColumn(name = \"permission_id\")}) private Set

What is the difference between DAO and Repository patterns?

こ雲淡風輕ζ 提交于 2019-11-26 04:02:46
问题 What is the difference between Data Access Objects (DAO) and Repository patterns? I am developing an application using Enterprise Java Beans (EJB3), Hibernate ORM as infrastructure, and Domain-Driven Design (DDD) and Test-Driven Development (TDD) as design techniques. 回答1: DAO is an abstraction of data persistence . Repository is an abstraction of a collection of objects . DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain,

Confusion: @NotNull vs @Column(nullable = false)

妖精的绣舞 提交于 2019-11-26 02:28:03
问题 When they appear on a field/getter of an @Entity , what is the difference between them? (I persist the Entity through Hibernate ). What framework and/or specification each one of them belongs to? @NotNull is located within javax.validation.constraints . In the javax.validation.constraints.NotNull javadoc it says The annotated element must not be null but it does not speak of the element\'s representation in the database, so why would I add the constraint nullable=false to the column? 回答1: