gpt4 book ai didi

java - 为 ActiveJdbc 模型定义类抛出异常

转载 作者:行者123 更新时间:2023-12-01 09:02:27 28 4
gpt4 key购买 nike

当我尝试使用defineClass(findClass URLClassLoader或 self 实现类加载器)连接我的模型类时,发生异常。

任何其他方法都正在加载我的模型,但不允许动态重载类。

@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

最佳答案

我在 MetaModels.java 中更改了 getTableName 并且它起作用了!

String getTableName(Class<? extends Model> modelClass) {
MetaModel mm = null;
for (Map.Entry<Class<? extends Model>, MetaModel> entry : metaModelsByClass.entrySet()){
if(modelClass.getName().equals(entry.getKey().getName()))
mm = entry.getValue();
}
return mm == null ? null : mm.getTableName();
}

原函数:

String getTableName(Class<? extends Model> modelClass) {
MetaModel mm = metaModelsByClass.get(modelClass);
return mm == null ? null : mm.getTableName();
}

我建议不要使用类作为键或更改选择逻辑。

关于java - 为 ActiveJdbc 模型定义类抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41568228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com