[JBoss Seam] - @In EntityManager is null when create an object from named b
by cftdanny
If there is a bean named database, which @In entity manager:
@Name("database")
| @AutoCreate
| public class Database {
|
| @In
| private EntityManager entityManager;
|
| public Table createTable() {
| return new Table(this);
| }
|
| public User getUser(Long id) {
| return this.entityManager.find(User.class, id);
| }
| }
And a table (like DAO, just work with User table):
public class Table {
|
| private Database database;
|
| public Table(Database database) {
| this.database = database;
| }
|
| public User getUser() {
| return this.database.getUser(0L);
| }
| }
If create table directly in bean, it will be ok, but if create table from Database, the entityManager in Database will be null. The following is test code:
public class Test() {
|
| @In
| private Database database;
|
| public void execute() {
| // The following code execute successfully
| Table t1 = new Table(this.database);
| User user1 = t1.getUser();
| System.out.println(user1.toString());
|
| // The following code will cause error
| // The entitymanager of database is null
| Table t2 = this.database.createTable();
| User user2 = t2.getUser();
| }
| }
Is there limited that we can not create a child bean from parent bean if child bean reference to parent bean?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074265#4074265
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074265
18Â years, 11Â months
[EJB 3.0] - Re: need help for Transaction Attribute Types
by ALRubinger
Sorry everyone for the delayed response, I must not have set this Thread to send emails when updated.
The Extended Persistence Context is 1/2 the equation, because if your EM is flushing automatically on any select/update/insert/delete events or when the Tx is committed, you're still going to have the same problems you're seeing.
@PersistenceContext(type=PersistenceContextType.EXTENDED, properties=@PersistenceProperty(name="org.hibernate.flushMode", value="MANUAL"))
| EntityManager oracleManager;
...and then flush the EM only when you want to; give that a spin?
Disclaimer: Haven't personally done this in a couple months; the code samples may be off but the theory should be spot-on. Use an Extended Persistence Context, and ensure that you are manually flushing it, and it's not getting flushed automatically by the container.
Mind reporting back your results? Curious to see if this path will work for you, and if I'm correct in assuming that the container is still flushing your EM for you.
S,
ALR
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074262#4074262
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074262
18Â years, 11Â months
[JBoss Seam] - EL functions library
by mgrouch
Here are few additional EL functions I couldn't live without.
(Might be they could be included into Seam code base)
| package org.el.func;
|
| import java.lang.reflect.InvocationTargetException;
| import java.text.SimpleDateFormat;
| import java.util.Collection;
| import java.util.Date;
|
| import org.apache.commons.beanutils.PropertyUtils;
| import org.jboss.seam.Entity;
| import org.jboss.seam.core.Expressions;
|
| public class SeamFunc {
|
| public static Object getId(Object bean) {
| Class<?> clazz = bean.getClass();
| if (!clazz.isAnnotationPresent(javax.persistence.Entity.class)) {
| // this better be instrumented proxy class
| clazz = clazz.getSuperclass();
| }
| return Entity.forClass(clazz).getIdentifier(bean);
| }
|
| public static String toString(Object obj) {
| return "" + obj;
| }
|
| public static Object[] toArray(Collection<Object> collect) {
| if (collect == null) {
| return null;
| } else {
| return collect.toArray();
| }
| }
|
| public static int size(Collection<Object> collect) {
| if (collect == null) {
| return 0;
| } else {
| return collect.size();
| }
| }
|
| public static boolean isBlank(String str) {
| if (str == null) {
| return true;
| } else {
| return "".equals(str.trim());
| }
| }
|
| public static String trunc(String str, int len) {
| if (str == null) {
| return null;
| } else {
| return str.substring(0, len);
| }
| }
|
| public static String concat(String... strings) {
| StringBuilder buff = new StringBuilder();
| if (strings != null) {
| for (String str : strings) {
| buff.append(str);
| }
| }
| return buff.toString();
| }
|
| public static boolean matches(String str, String regex) {
| if (str == null) {
| return false;
| } else {
| return str.matches(regex);
| }
| }
|
| public static String formatDate(Date date, String pattern) {
| if (date == null) {
| return null;
| }
| else {
| return new SimpleDateFormat(pattern).format(date);
| }
| }
|
| public static String getProperty(String propName) {
| return System.getProperty(propName);
| }
|
| public static String getEnv(String varName) {
| return System.getenv(varName);
| }
|
| public static Object getBeanProperty(Object bean, String propName) {
| Object obj = null;
| try {
| obj = PropertyUtils.getProperty(bean, propName);
| } catch (IllegalAccessException ex) {
| throw new RuntimeException(ex);
| } catch (InvocationTargetException ex) {
| throw new RuntimeException(ex);
| } catch (NoSuchMethodException ex) {
| throw new RuntimeException(ex);
| }
| return obj;
| }
|
| public static Object evalEl(String expression) {
| String framedExpr = "#{" + expression + "}";
| Object value = Expressions.instance().createValueBinding(framedExpr).getValue();
| return value;
| }
| }
|
| /**
| * Licensed under the Common Development and Distribution License,
| * you may not use this file except in compliance with the License.
| * You may obtain a copy of the License at
| *
| * http://www.sun.com/cddl/
| *
| * Unless required by applicable law or agreed to in writing, software
| * distributed under the License is distributed on an "AS IS" BASIS,
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
| * implied. See the License for the specific language governing
| * permissions and limitations under the License.
| */
|
| package org.el.func;
|
| import java.lang.reflect.Method;
| import java.lang.reflect.Modifier;
| import java.util.HashMap;
| import java.util.Map;
|
| import javax.faces.FacesException;
|
| import com.sun.facelets.tag.TagConfig;
| import com.sun.facelets.tag.TagHandler;
| import com.sun.facelets.tag.TagLibrary;
|
| public class FnLibrary implements TagLibrary {
|
| public final static String Namespace = "http://org.el.func/func";
|
| private final Map<String, Method> fns = new HashMap<String, Method>();
|
| public FnLibrary() {
| super();
| try {
| Method[] methods = SeamFunc.class.getMethods();
| putMethods(methods);
| } catch (Exception e) {
| throw new RuntimeException(e);
| }
| }
|
| private void putMethods(Method[] methods) {
| for (int i = 0; i < methods.length; i++) {
| if (Modifier.isStatic(methods.getModifiers())) {
| fns.put(methods.getName(), methods);
| }
| }
| }
|
| public boolean containsNamespace(String ns) {
| return Namespace.equals(ns);
| }
|
| public boolean containsTagHandler(String ns, String localName) {
| return false;
| }
|
| public TagHandler createTagHandler(String ns, String localName,
| TagConfig tag) throws FacesException {
| return null;
| }
|
| public boolean containsFunction(String ns, String name) {
| if (Namespace.equals(ns)) {
| return this.fns.containsKey(name);
| }
| return false;
| }
|
| public Method createFunction(String ns, String name) {
| if (Namespace.equals(ns)) {
| return (Method) this.fns.get(name);
| }
| return null;
| }
|
| public static void main(String[] argv) {
| FnLibrary lib = new FnLibrary();
| System.out.println(lib.containsFunction(FnLibrary.Namespace, "isBlank"));
| }
| }
|
META-INF/elfunc.taglib.xml
| <facelet-taglib>
| <library-class>org.el.func.FnLibrary</library-class>
| </facelet-taglib>
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074260#4074260
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074260
18Â years, 11Â months
[Persistence, JBoss/CMP, Hibernate, Database] - Atomic Transaction Help!!
by lemboy4
Hi, this question is a bit involved; though all I can offer is my eternal gratitude to somebody who helps me avoid wasting even more hours on understanding this...
I am avoiding using Entity beans for now, and created a set of methods that directly call SQL, something like....
class DBAccess {
method1(parameters for SQL) {
open connection();
execute a statement with these parameters...
close connection();
}
...
}
Now, I have a message driven bean listening for JMS, and when it receives a message it calls a method in DBAccess that looks like:
method {
check_if_object_exists_in_DB();
if(doesnt_exist) {
create_object_in_DB();
do_other_stuff_involving_new_object_in_DB();
}
}
Now, JBoss won't treat this method as atomic no matter what...I get duplicates in the DB in a way that looks certainly like multiple threads are seeing doesnt_exist and creating.
I've tried many things and rejected them:
-- Consolidate into one statement? Too complicated, has to be done in multiple queries
-- Locking database table? This just pushes the problem a layer lower, and ruins my whole nice set of SQL methods (used in other places) that just do one thing each, since I believe they could no longer open and close connections individually and would have to all be put together to operate under one connection with the lock
-- Synchronizing around this method....supposedly very much not allowed in JBoss, and besides the method items then execute in the correct order but STILL I end up with duplicates
If I were using Entity beans I think the thing to do here would be open a "transaction" at the start of the big method that calls all the others... Am I just going about this all wrong? I am new to JBoss, is there some brilliant way to structure methods that just execute SQL so this problem never comes up??
Thanks for any help provided!!!!
-Chris
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074256#4074256
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074256
18Â years, 11Â months