Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好 queryFirst 关联查询报错 #100

Open
forgottor opened this issue Jan 14, 2021 · 8 comments
Open

你好 queryFirst 关联查询报错 #100

forgottor opened this issue Jan 14, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@forgottor
Copy link

MeeMeeting m = MeeMeeting.queryFirst("mee_id > ?", MeeMeeting.HAS_MANY_MEE_CHATS,1L)

@relation(relationType = RelationType.HAS_MANY,foreignKey = "meeting_id",foreignFieldName = "meetingId")
private List meeChats;

Invalid argument value: java.io.NotSerializableException Query: SELECT * FROM mee_meeting WHERE mee_id > ? Parameters: [com.github.braisdom.objsql.relation.Relationship@3d534f26, 1]

@catchex
Copy link
Member

catchex commented Jan 14, 2021

Relationship 是一个数组,正确的用法如下:

Relationship[] relationships = new Relationship[]{ MeeMeeting.HAS_MANY_MEE_CHATS };
MeeMeeting m = MeeMeeting.queryFirst("mee_id > ?", relationships, 1L);

@forgottor
Copy link
Author

麻烦再问一下, 设置Queryable得时候,怎么对应到字段,现在都是用实体去组sql
MeeMeeting.queryByMeeName("11", new Relationship[]{ MeeMeeting.HAS_MANY_MEE_CHATS });

@Queryable
@Column(name = "mee_name")
private String meeName;

  Unknown column 'meeName' in 'where clause'

@catchex
Copy link
Member

catchex commented Jan 14, 2021

@forgottor 请贴出你生成的SQL,还有你使用的版本信息,Tables.java#144 行的代码如下,你也可以调试一下,逻辑应该是对的:

public static final String getColumnName(Class tableClass, String fieldName) {
        try {
            Field field = tableClass.getDeclaredField(fieldName);
            Column column = field.getDeclaredAnnotation(Column.class);

            if (column != null) {
                if (!StringUtil.isEmpty(column.name())) {
                    return column.name();
                }
            }

            return tableNameEncoder.getColumnName(tableClass, field.getName());
        } catch (NoSuchFieldException ex) {
            throw new IllegalStateException(ex.getMessage(), ex);
        }
    }

@forgottor
Copy link
Author

你好 这是我用得版本和自动生成得代码

com.github.braisdom
objsql-springboot
1.3.4

public static final MeeMeeting queryByMeeName(String value, Relationship... relations) throws SQLException {
Query query = createQuery();
String columnName = Tables.getColumnName(MeeMeeting.class, "meeName");
query.where(String.format("%s = ?", "meeName"), new Object[]{value});
return (MeeMeeting)query.queryFirst(relations);
}

@catchex
Copy link
Member

catchex commented Jan 14, 2021

你可以在 getColumnName 方法中,断点调试一下,跟踪一下结果就应该知道原因了,晚点,我在本地测试一下,并增加一个这块的单元测试

@forgottor
Copy link
Author

getColumnName 得结果是对的mee_name,可是这块代码
query.where(String.format("%s = ?", "meeName"), new Object[]{value}); 已经把查询得字段写死了啊

@catchex catchex self-assigned this Jan 14, 2021
@catchex catchex added the bug Something isn't working label Jan 14, 2021
@catchex
Copy link
Member

catchex commented Jan 14, 2021

对不起,是我的生成代码的错,之前单元测试没有覆盖这种场景,我修改一下代码,今晚发个最新版本。

@forgottor
Copy link
Author

好的 麻烦了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants