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
No description
No description
No description
No description
No description
31 Replies
JavaBot
JavaBot7d ago
This post has been reserved for your question.
Hey @red! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
red
redOP7d ago
I will get the stacktrace
ayylmao123xdd
ayylmao123xdd7d ago
can you try something like this
user

@OneToMany(mappedBy = "user")
private List<Expense> expenses;
user

@OneToMany(mappedBy = "user")
private List<Expense> expenses;
expense

@ManyToOne
private User user;
expense

@ManyToOne
private User user;
oh you are using jdbc scratch that then
red
redOP7d ago
No description
No description
dan1st
dan1st7d ago
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?
red
redOP7d ago
The stacktrace exceeds Discord character limit
dan1st
dan1st7d ago
sending it as a text file should be ok as well
red
redOP7d ago
No, I'm using the ListCrudRepository interface I'll do it
dan1st
dan1st7d ago
Does that do anything? and can you also show the full DDL (ideally as text)? What DBMS are you using?
red
redOP7d ago
MariaDb I'll change now, give me a moment
dan1st
dan1st7d ago
Did you tell Spring about that anywhere?
red
redOP7d ago
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 );
dan1st
dan1st7d ago
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 happens
red
redOP7d ago
It didn't work
dan1st
dan1st7d ago
What happens?
red
redOP7d ago
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 );
dan1st
dan1st7d ago
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")?
red
redOP7d ago
I just did this but I got the same error Yes, gimme a moment
dan1st
dan1st7d ago
Im mainly interested in what happens with that
red
redOP7d ago
Do you mean the "@MappedCollections", right?
dan1st
dan1st7d ago
yes ah
dan1st
dan1st7d ago
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
@MappedCollection(idColumn = "users_id", keyColumn="user_key")
private List<Expense> expenses;
@MappedCollection(idColumn = "users_id", keyColumn="user_key")
private List<Expense> expenses;
red
redOP7d ago
Oh I think I got it
dan1st
dan1st7d ago
and the corresponding SQL would be
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_id BIGINT,
users_key INT,
FOREIGN KEY (users_id) REFERENCES users(id) ON DELETE CASCADE
);
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_id BIGINT,
users_key INT,
FOREIGN KEY (users_id) REFERENCES users(id) ON DELETE CASCADE
);
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 key
dan1st
dan1st7d ago
GitHub
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...
red
redOP7d ago
It works well, thanks
JavaBot
JavaBot7d ago
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.
JavaBot
JavaBot7d ago
Post Closed
This post has been closed by <@1087022845957242941>.

Did you find this page helpful?