Help me with spring JDBC
Hello, I'm using spring data JDBC to manage two entities, but I am getting a rather strange exception when testing the UserRepository. Spring is trying to map the "users_key" attribute of the "expense" table, but this attribute is nonexistent. I'm sure the core problem is in the mapping of the "expense" attribute of the User entity, because when I annotate the attribute as @Transient, the exception doesn't occur.
Below are the images of the entities, the SQL script to generate the tables, the test code, the repository and the stack trace
31 Replies
⌛
This post has been reserved for your question.
Hey @red! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
I will get the stacktrace
can you try something like this
oh you are using jdbc
scratch that then
Can you actually copy the full stack trace in text form instead of sending images of it?
What happens if you change the name of
user_id
to users_key
in the DB?
Do you have your own custom repository implementation?The stacktrace exceeds Discord character limit
sending it as a text file should be ok as well
No, I'm using the ListCrudRepository interface
I'll do it
Does that do anything?
and can you also show the full DDL (ideally as text)?
What DBMS are you using?
MariaDb
I'll change now, give me a moment
Did you tell Spring about that anywhere?
CREATE TABLE IF NOT EXISTS users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS expense (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(255) NOT NULL,
value DECIMAL(10, 2) NOT NULL,
date DATE NOT NULL,
user_id BIGINT,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
actually when doing that, also remove your
@MappedCollection
and see what happens
if you want to keep it that way, you can revert it afterwards and check what happensIt didn't work
What happens?
The DLL changed: CREATE TABLE IF NOT EXISTS users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS expense (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(255) NOT NULL,
value DECIMAL(10, 2) NOT NULL,
date DATE NOT NULL,
users_key BIGINT,
FOREIGN KEY (users_key) REFERENCES users(id) ON DELETE CASCADE
);
Can you copy the statement and execute it in the DB manually?
oh it states the reasoning
Unknown column 'expense.users' in 'WHERE'oh I think the original one would be fine if you used keyColumn Can you try
@MappedColumn(keyColumn = "users_key")
?I just did this but I got the same error
Yes, gimme a moment
Im mainly interested in what happens with that
Do you mean the "@MappedCollections", right?
yes
ah
it seems if you use a
List
, you might need both an idColumn
and a mappedColumn
because the elements are ordered
and an equivalent thing in the DB
I think that if you used Set
, your initial approach should have worked
but with List
, you would need something like
Oh I think I got it
and the corresponding SQL would be
or something like that
(I think, I'm not that experienced with Spring JDBC automatically created repositories)
and I think by default, Spring would name the
users_id
just user
or something like that
alternatively I think you could use Collection
instead of List
if you don't want ordering - then you could skip the part with the keyGitHub
Support List without @MappedCollection(keyColumn=) [DATAJDBC-630] ·...
Veli-Pekka Muli opened DATAJDBC-630 and commented Currently the only way to have collections that don't have an order column in the database is to make the collection a Set. This is limiting as...
It works well, thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@1087022845957242941>.