Why is am I getting a duplicate class mapping error - nhibernate

in my nhibernate session helper class, I load my entities into the configuration like:
static NHibernateHelper()
{
try
{
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof (Category).Assembly);
cfg.AddAssembly(typeof (Product).Assembly);
SessionFactory = cfg.Configure().BuildSessionFactory();
}
catch (Exception ex)
{
}
}
It works fine if I only have 1 cfg.AddAssembly, but loading both Category and Product results in an error?

Are they both in a same assembly (Category and Product). If they are, then you just need one AddAssembly.

i think you are calling Configure twice try removing the first cfg.Configure();
this is how it should look :
static NHibernateHelper(){
try{
Configuration cfg = new Configuration();
cfg.AddAssembly(typeof (Category).Assembly);
cfg.AddAssembly(typeof (Product).Assembly);
SessionFactory = cfg.Configure().BuildSessionFactory();
}
catch (Exception ex){
}}

Related

How to do programmatic indexing with infinispan-13?

How is it possible to do programmatic definition of indexes for pojos with infinispan-13? According to the online documentation here:
the code hasn't changed. However, the classes referred to no longer exist in hibernate-6, which is used by infinispan-13.
Note that it is not possible to use annotations on the pojo class to be indexed because it is used elsewhere by code that cannot have any dependency on infinispan or hibernate etc, so I need to pursue the programmatic route that used to work in infinispan-11.
This is the code that currently works in infinispan-11:
EmbeddedCacheManager cacheManager = new DefaultCacheManager(new GlobalConfigurationBuilder().jmx().build());
SearchMapping mapping = new SearchMapping();
mapping.entity(MyData.class).indexed().property("expiry", ElementType.FIELD).field();
Properties properties = new Properties();
properties.put(Environment.MODEL_MAPPING, mapping);
properties.put("hibernate.search.default.indexBase", "/some/path");
Configuration dcc = cacheManager.getDefaultCacheConfiguration();
ConfigurationBuilder b = new ConfigurationBuilder();
if (dcc != null)
b = b.read(dcc);
b.indexing().addIndexedEntity(MyData.class).withProperties(properties);
I also use Infinispan 13.0.6. This configuration works just fine.
Here is my configuration:
ConfigurationBuilder builder = new ConfigurationBuilder();
Properties p = new Properties();
try(Reader r = new FileReader("/srv/ws-emporium/hotrod-client.properties")) {
p.load(r);
builder.withProperties(p);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
remoteCacheManager = new RemoteCacheManager(builder.build());
cache = remoteCacheManager.getCache("teste15Fev22");
My properties file content:
infinispan.client.hotrod.server_list = server1:11222;server2:11322
infinispan.client.hotrod.auth_username=admin
infinispan.client.hotrod.auth_password=password
infinispan.client.hotrod.connection_pool.max_active = 10
infinispan.client.hotrod.connection_pool.exhausted_action = WAIT
infinispan.client.hotrod.connection_pool.max_wait = 1
infinispan.client.hotrod.connection_pool.min_idle = 20
infinispan.client.hotrod.connection_pool.min_evictable_idle_time = 300000
infinispan.client.hotrod.connection_pool.max_pending_requests = 20
infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller
infinispan.client.hotrod.java_serial_allowlist=.*

Fluently.Configure throws "Database was not configured through Database method" exception

I am trying to get an in-house application up and running for debugging and when I start the solution, it fails calling the Fluently.Configure() method. Digging into the exception, I see the potential reason as "Database was not configured through Database method."
public static ISessionFactory IdCardSessionFactoryWeb
{
get
{
if (_idCardSessionFactoryWeb != null) return _idCardSessionFactoryWeb;
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
x.Driver<SybaseAdoNet4Driver>();
x.Dialect<SybaseAdoNet4Dialect>();
x.Batcher<SybaseASEBatcherFactory>();
});
_idCardSessionFactoryWeb = Fluently.Configure(cfg)
.ExposeConfiguration(c => c
.SetInterceptor(new ABCInterceptor())
.SetProperty(Environment.ShowSql, "true")
.SetProperty(Environment.WrapResultSets, "true")
.SetProperty("command_timeout", timeout)
.SetProperty("current_session_context_class", "web"))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Batch>())
.BuildSessionFactory();
return _idCardSessionFactoryWeb;
}
}
Looks like I needed to update the nuget package, and then restart my solution.

Unable to cleanup Infinispan DefaultCacheManager in state FAILED

I am getting this Exception when trying to restart CacheManager, that failed to start.
Caused by: org.infinispan.jmx.JmxDomainConflictException: ISPN000034: There's already a JMX MBean instance type=CacheManager,name="DefaultCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element
at org.infinispan.jmx.JmxUtil.buildJmxDomain(JmxUtil.java:53)
I think it's a bug, but am I correct?
The version used is 9.0.0.Final.
EDIT
The error can be seen using this code snippet.
import org.infinispan.configuration.cache.*;
import org.infinispan.configuration.global.*;
import org.infinispan.manager.*;
class Main {
public static void main(String[] args) {
System.out.println("Starting");
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
global.transport()
.clusterName("discover-service-poc")
.initialClusterSize(3);
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.clustering().cacheMode(CacheMode.REPL_SYNC);
DefaultCacheManager cacheManager = new DefaultCacheManager(global.build(), builder.build(), false);
try {
System.out.println("Starting cacheManger first time.");
cacheManager.start();
} catch (Exception e) {
e.printStackTrace();
cacheManager.stop();
}
try {
System.out.println("Starting cacheManger second time.");
System.out.println("startAllowed: " + cacheManager.getStatus().startAllowed());
cacheManager.start();
System.out.println("Nothing happening because in failed state");
System.out.println("startAllowed: " + cacheManager.getStatus().startAllowed());
} catch (Exception e) {
e.printStackTrace();
cacheManager.stop();
}
cacheManager = new DefaultCacheManager(global.build(), builder.build(), false);
cacheManager.start();
}
}

Jena model.setNsPrefixes through java.lang.NullPointerException

Here is the code:
*Location location = Location.create("target/DBLP");
Dataset dataset = TDBFactory.createDataset(location);
dataset.begin(ReadWrite.READ);
Model model = dataset.getDefaultModel();
HashMap<String, String> prifixMap = new HashMap<>();
prifixMap.put("rdf","<http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
try{
model.setNsPrefixes(prifixMap);
}
catch (Exception e)
{
e.printStackTrace();
}*
It always throws a java.lang.NullPointerException when it executes the line of model.setNsPrefixes(), and the detail printStackTrace is as following:
java.lang.NullPointerException
at org.apache.jena.tdb.store.DatasetPrefixesTDB.readPrefix(DatasetPrefixesTDB.java:86)
at org.apache.jena.sparql.graph.GraphPrefixesProjection.get(GraphPrefixesProjection.java:101)
at org.apache.jena.sparql.graph.GraphPrefixesProjection.set(GraphPrefixesProjection.java:79)
at org.apache.jena.shared.impl.PrefixMappingImpl.setNsPrefix(PrefixMappingImpl.java:75)
at org.apache.jena.shared.impl.PrefixMappingImpl.setNsPrefixes(PrefixMappingImpl.java:163)
at org.apache.jena.rdf.model.impl.ModelCom.setNsPrefixes(ModelCom.java:1043)
at ReadTransaction.(ReadTransaction.java:32)
at ReadTransaction.main(ReadTransaction.java:133)
I have checked jena API, and I could not find any solution.
Thanks for any answer!
The following code worked for me:
Location location = Location.create("target/DBLP");
Dataset dataset = TDBFactory.createDataset(location);
dataset.begin(ReadWrite.WRITE); // changed from READ to WRITE
Model model = dataset.getDefaultModel();
HashMap<String, String> prefixMap = new HashMap<>();
prefixMap.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); // removed '<' and '>'
try {
model.setNsPrefixes(prefixMap);
} catch (Exception e) {
e.printStackTrace();
}
The key is to change the transaction type from READ to WRITE. You are trying to write data so you must be in a write transaction!

EJB lookup from PostConstruct method in GlassFish 3.1.2

Deploying ear on Glassfish 3.1.2 and trying to lookup EJB bean from #PostConstruct method in web service:
#PostConstruct
public void init()
{
try
{
InitialContext initialContext = new InitialContext();
browserService = (IBrowserService) initialContext.lookup("java:comp/env/BrowserService");
}
catch (NamingException ex)
{
Logger.getLogger(BrowserWS.class.getName()).log(Level.SEVERE, null, ex);
}
}
and got
[#|2012-04-25T10:15:54.768+0300|SEVERE|glassfish3.1.2|browser.service.BrowserWS|_ThreadID=22;_ThreadName=admin-thread-pool-4848(4
);|javax.naming.NamingException: Lookup failed for 'java:comp/env/BrowserService' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpri
se.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.ur
l.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
When i try do the same from web method all works fine.
#WebMethod
public XmlLookupResult lookup(
#WebParam(name = "inLookupId", mode = WebParam.Mode.IN) String id,
#WebParam(name = "inLookupParams", mode = WebParam.Mode.IN) List<String> params,
#WebParam(name = "inFetchLimit", mode = WebParam.Mode.IN) int limit)
{
try
{
InitialContext initialContext = new InitialContext();
browserService = (IBrowserService) initialContext.lookup("java:comp/env/BrowserService");
}
catch (NamingException ex)
{
Logger.getLogger(BrowserWS.class.getName()).log(Level.SEVERE, null, ex);
}
return browserService.lookup(id, params, limit);
}
So, why i can't lookup beans from #PostConstruct method?
From specification whe have JNDI access to java:comp/env from PostConstruct...
Thanks
UPD:
BrowserService declared in web.xml as
<ejb-local-ref>
<ejb-ref-name>BrowserService</ejb-ref-name>
<local>browser.service.IBrowserService</local>
</ejb-local-ref>