How could I simplify this

public interface Dao<K, S extends Key<K>> { void update(S item); void insert(S item); void delete(K key); S select(K key); List<S> selectAll(); } public interface Dao<K, T, S extends Key<K, T>> { void update(S item); void insert(S item); void delete(K key1, T key2); S select(K key1, T key2); List<S> selectAll(); } public interface Dao<K, T, I S extends Key<K, T, I>> { void update(S item); void insert(S item); void delete(K key1, T key2, I key3); S select(K key1, T key2, I key3); List<S> selectAll(); }
49 Replies
JavaBot
JavaBot•7mo ago
⌛ This post has been reserved for your question.
Hey @userexit! 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 closed 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.
JavaBot
JavaBot•7mo ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
userexit
userexitOP•7mo ago
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}

public interface Dao<K, T, S extends Key<K, T>> {
void update(S item);
void insert(S item);
void delete(K key1, T key2);
S select(K key1, T key2);
List<S> selectAll();
}

public interface Dao<K, T, I, S extends Key<K, T, I>> {
void update(S item);
void insert(S item);
void delete(K key1, T key2, I key3);
S select(K key1, T key2, I key3);
List<S> selectAll();
}
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}

public interface Dao<K, T, S extends Key<K, T>> {
void update(S item);
void insert(S item);
void delete(K key1, T key2);
S select(K key1, T key2);
List<S> selectAll();
}

public interface Dao<K, T, I, S extends Key<K, T, I>> {
void update(S item);
void insert(S item);
void delete(K key1, T key2, I key3);
S select(K key1, T key2, I key3);
List<S> selectAll();
}
JavaBot
JavaBot•7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
userexit
userexitOP•7mo ago
P
dan1st
dan1st•7mo ago
This is not valid syntax or actually why do you have all these 3 classes? Is the first one not enough? you could create a void delete(List<? extends K> keys); if you want a method for deleting multiple keys
userexit
userexitOP•7mo ago
public class StationsDto extends Key<Integer> {...}

public class StationsDao implements Dao<Integer, StationsDto> {
void delete(Integer key) {...}
void insert(StationsDto item) {...}
...
}
public class StationsDto extends Key<Integer> {...}

public class StationsDao implements Dao<Integer, StationsDto> {
void delete(Integer key) {...}
void insert(StationsDto item) {...}
...
}
and
public class StopsDto extends Key<Integer, Integer> {...}

public class StopsDao implements Dao<Integer, Integer, StopsDto> {
void delete(Integer key1, Integer key2) {...}
void insert(StopsDto item) {...}
...
}
public class StopsDto extends Key<Integer, Integer> {...}

public class StopsDao implements Dao<Integer, Integer, StopsDto> {
void delete(Integer key1, Integer key2) {...}
void insert(StopsDto item) {...}
...
}
I have a Stations table in my db that has a primary key that consists of 1 column whereas in Stops table the primary key is consisted of multiple columns
dan1st
dan1st•7mo ago
In the first one, there's no need to override the methods in the 2., why do you want a delete method with multiple keys when you can pass a list (or at least a vararg)
userexit
userexitOP•7mo ago
why ?
dan1st
dan1st•7mo ago
Because the methods are already present in the super interface so the child interface inherits them So why override it?
userexit
userexitOP•7mo ago
because my primary key for the Stops table is consisted of multiple columns, 2 in this case, both integers, so then I can do: String query = "DELETE FROM STOPS WHERE first_id = ? AND second_id = ?" ... its a class
dan1st
dan1st•7mo ago
ah ok I overlooked that
userexit
userexitOP•7mo ago
StationsDto represents a station (data transfer object) StationsDao connects to the database and does CRUD
No description
dan1st
dan1st•7mo ago
In that case, make a class for the primary key or a record
public record StopsKey(int firstStopId, int secondStopId) {}
public record StopsKey(int firstStopId, int secondStopId) {}
and
public class StopsDao implements Dao<StopsKey, StopsDto> {
...
}
public class StopsDao implements Dao<StopsKey, StopsDto> {
...
}
userexit
userexitOP•7mo ago
and then I can use the version
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}
` and if I have another table that needs for example 4 columns as pk i just make a new record everytime ? @dan1st | Daniel sounds like a good solution to me
dan1st
dan1st•7mo ago
yes yeah btw you can also use records for your DTOs if you want to
userexit
userexitOP•7mo ago
but can you override equals and hashcode in a record ?
dan1st
dan1st•7mo ago
yes but it gives you a good equals/hashCode with all components by default So in most cases, you don't have to override it
userexit
userexitOP•7mo ago
well the only problem is how do I setup the interface now to accept StopsKey, StopsDto
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}
public interface Dao<K, S extends Key<K>> {
void update(S item);
void insert(S item);
void delete(K key);
S select(K key);
List<S> selectAll();
}
or should I make StopsPK extends Key of some sort ?
dan1st
dan1st•7mo ago
wouldn't that need to be the DTO?
userexit
userexitOP•7mo ago
No description
userexit
userexitOP•7mo ago
but like what do I put in something<T>
dan1st
dan1st•7mo ago
Why? just do <T,S> unless Key has something important and if it's an interface, records can implement interfaces
userexit
userexitOP•7mo ago
no it s just a record itself lol
dan1st
dan1st•7mo ago
?
userexit
userexitOP•7mo ago
if u do
dan1st
dan1st•7mo ago
Is there any reason to not just use Dao<T,S>?
userexit
userexitOP•7mo ago
Dao<T, S> ur not making sure that T is a key of some sort to S if u do
dan1st
dan1st•7mo ago
ah
userexit
userexitOP•7mo ago
Dao<T, S extends something<T>> ur making sure S extends Key<T>
dan1st
dan1st•7mo ago
yeah you can make an interface for that
userexit
userexitOP•7mo ago
empty interface > ?
dan1st
dan1st•7mo ago
either empty or with a K getKey() method your choice that's if you want all your DTOs to have a getKey method in common
userexit
userexitOP•7mo ago
No description
userexit
userexitOP•7mo ago
No description
userexit
userexitOP•7mo ago
you meant this ?
dan1st
dan1st•7mo ago
What's Key?
userexit
userexitOP•7mo ago
empty interface in the end i just made a StationsPK to keep a main program structure
dan1st
dan1st•7mo ago
I thought about
public interface Dto<K>{
//if you want to, not necessary
//K getKey();
}
public interface Dao<K,T implements Dto<K>>{}

public record StationsDto(Integer key, WhateverOtherParameters youHave) implements Dto<Integer>{}
public class StationsDao implements Dao<Integer, StationsDto>{}


public record StopKey(Integer firstStationId, Integer secondStationId){}
public record StopDto(StopKey key, OtherParameters here) implements Dto<StopKey>{}
public class StopDao implements Dao<StopKey, StopDto>{}
public interface Dto<K>{
//if you want to, not necessary
//K getKey();
}
public interface Dao<K,T implements Dto<K>>{}

public record StationsDto(Integer key, WhateverOtherParameters youHave) implements Dto<Integer>{}
public class StationsDao implements Dao<Integer, StationsDto>{}


public record StopKey(Integer firstStationId, Integer secondStationId){}
public record StopDto(StopKey key, OtherParameters here) implements Dto<StopKey>{}
public class StopDao implements Dao<StopKey, StopDto>{}
but what you did looks ok as well
userexit
userexitOP•7mo ago
No description
No description
userexit
userexitOP•7mo ago
can i return its parent ?
JavaBot
JavaBot•7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st•7mo ago
?
userexit
userexitOP•7mo ago
nvm was a dumb question of the moment it was 9 am for me
userexit
userexitOP•7mo ago
No description
dan1st
dan1st•7mo ago
So you no longer have that question?
userexit
userexitOP•7mo ago
yeah no longer
dan1st
dan1st•7mo ago
Is that late (i.e. before going to bed) or early (after waking up)? xd (yes that message is sarcastic)
JavaBot
JavaBot•7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server