When I try to connect my model class using defineClass (findClass URLClassLoader or self-realization ClassLoader), occurs exception.
Any other methods are being loaded my model, but do not allow dynamic overloading the classes.
@Table("accounts")
@BelongsToParents({
@BelongsTo(parent = Customer.class, foreignKeyName = "Customer"),
@BelongsTo(parent = Currency.class, foreignKeyName = "Currency")
})
public class Account extends Document{
public static DocFields prepareDocument(...){
... fields = new ...(getTableName());
...
}
...
}
public abstract class Document extends Model{
public static DocFields prepareDocument(Session session){
return null;
}
...
}
public class DynCLoader extends ClassLoader {
public DynCLoader(ClassLoader parentClass) {
super(parentClass);
}
@Override
public Class<?> loadClass(String className) throws ClassNotFoundException {
if(StrFuncs.isEmpty(className)){
throw new ClassNotFoundException("Ошибка в модуле DynCLoader, в функцие loadClass. Детали: className is empty");
}
try {
...
if(...){ // Загрузка нового, неопределенного класса
Class<?> clazz;
try{
String url = "file:"+...+".class";
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
InputStream input = connection.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int data = input.read();
while(data != -1){
buffer.write(data);
data = input.read();
}
input.close();
byte[] classData = buffer.toByteArray();
clazz = defineClass(className, classData, 0, classData.length);
...
return clazz;
}
catch (FileNotFoundException ex) {
clazz = Class.forName(className);
...
return clazz;
}
catch (MalformedURLException ex) {
...
return null;
}
catch (Throwable ex) {
try{
clazz = Class.forName(className, true, this);
return clazz;
}
catch(ClassNotFoundException oErr){
...
}
...
return null;
}
}
else if(...) // Загрузка системного класса
return Class.forName(className);
else{
return Class.forName(className, true, this);
}
}
catch (ClassNotFoundException ex) {
throw ex;
}
}
}
Log:
[main] INFO org.javalite.activejdbc.DB - Opened connection: com.mysql.jdbc.JDBC4Connection@49fd9b
[main] INFO org.javalite.activejdbc.ConnectionsAccess - Attached connection named: default: to current thread: com.mysql.jdbc.JDBC4Connection@49fd9b. Extra info: jdbc:mysql://127.0.0.1:3306/***
[main] INFO org.javalite.activejdbc.Configuration - Load models from: file:/***/Product/***/Server/target/classes/activejdbc_models.properties
[main] INFO org.javalite.activejdbc.Configuration - Load models from: file:/***/Kernel/Server/target/classes/activejdbc_models.properties
[main] INFO org.javalite.activejdbc.Registry - Registered model: class kz.mwb.qupris.server.data.model.Account
***
[main] INFO org.javalite.activejdbc.Registry - Registered model: class kz.mwb.qupris.server.data.model.User
***
[main] INFO org.javalite.activejdbc.Registry - Fetched metadata for table: accounts
***
[main] INFO org.javalite.activejdbc.Registry - Fetched metadata for table: usertable
***
[main] INFO org.javalite.activejdbc.MetaModel - Association found: Customer ----------< Account, type: has-many
[main] INFO org.javalite.activejdbc.MetaModel - Association found: Account >---------- Customer, type: belongs-to
***
[main] INFO org.javalite.activejdbc.cache.QueryCache - MISS, "SELECT * FROM *** WHERE ***
***
[Thread-20] INFO org.javalite.activejdbc.DB - Opened connection: com.mysql.jdbc.JDBC4Connection@1db686
[Thread-20] INFO org.javalite.activejdbc.ConnectionsAccess - Attached connection named: default: to current thread: com.mysql.jdbc.JDBC4Connection@1db686. Extra info: jdbc:mysql://127.0.0.1:3306/***
[Thread-20] INFO org.javalite.activejdbc.LazyList - Query: "SELECT * FROM *** WHERE ***, took: 5 milliseconds
***
00:42:52 > 34 > UserLib > ERROR > java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at kz.mwb.qupris.server.tool.loader.Include.processMethod(Include.java:111)
at kz.mwb.qupris.server.userlib.doDoc.initMod(doDoc.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at kz.mwb.qupris.server.tool.loader.Include.processMethod(Include.java:111)
at kz.mwb.qupris.server.tool.loader.Include.EvalFunction(Include.java:31)
at kz.mwb.qupris.server.engine.ModEngine.MProcess(ModEngine.java:133)
at kz.mwb.qupris.server.engine.ModEngine.IProcess(ModEngine.java:85)
at kz.mwb.qupris.server.engine.ModEngine.XProcess(ModEngine.java:203)
at kz.mwb.qupris.server.engine.Task.ProcessRequest(Task.java:396)
at kz.mwb.qupris.server.engine.Task.GateWayLine(Task.java:162)
at kz.mwb.qupris.server.engine.Task.ConnectToGateWay(Task.java:326)
at kz.mwb.qupris.server.engine.Task.run(Task.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: org.javalite.activejdbc.DBException: failed to find metamodel for class kz.mwb.qupris.server.data.model.Account. Are you sure that a corresponding table exists in DB?
at org.javalite.activejdbc.Registry.getTableName(Registry.java:414)
at org.javalite.activejdbc.ModelDelegate.tableNameOf(ModelDelegate.java:326)
at kz.mwb.qupris.server.data.model.Account.getTableName(Account.java:2831)
at kz.mwb.qupris.server.data.model.Account.prepareDocument(Account.java:23)
... 20 more
I changed getTableName in a MetaModels.java and it worked!
Original function:
I suggest not to use the class as a key or change the selection logic.