Delete all objects from hashmap that match specific condition - java

Following situation. I have:
public class Management {
private HashMap<Integer, Book> allBooks = new HashMap<>();
public void deleteAllBooksFromOwner(Owner owner) {
}
public class Owner {
private String name;
// getters
}
public class Book {
private Owner owner;
// getters
}
}
I want to write a method in Management class to delete books:
public void deleteAllBooksFromOwner(Owner owner){
}
I don't know how to access the book owner, for my comparison.
Getters are available.

You can use removeIf
public void deleteAllBooksFromOwner(Owner owner) {
allBooks.entrySet().removeIf(entry -> entry.getValue().getOwner().equals(owner));
}

Related

Should I keep an enum attribute when it has always the same value as a result of a new inheritance?

I have these classes:
enum Brand {
FORD, FERRARI, TESLA, RENAULT;
}
public class Car {
Brand brand;
String plate;
...
}
//getters and setters
Imagine that for some reason, I need to make Car a superclass for two new classes: CombustionCar and ElectricCar. One of the new requierements is that ElectricCar's brand attribute must be always TESLA value and not any of the other ones values.
I've thougth some solutions:
I could keep Brand attr on superclass Car, and make ElectricCar constructor to set TESLA brand. But this way could allow me to set a new Brand after creating the object
public class ElectricCar extends Car {
public ElectricCar(...){
super(Brand.TESLA, ...);
}
ElectricCar ec = new ElectricCar(...);
ec.setBrand(Brand.FORD);
I can take Brand attr out from superclass and set it on both subclasses, but setting it in ElectricCar as a class attribute with a final so anyone would be able to set a new value
public class ElectricCar extends Car {
public static final Brand brand = Brand.TESLA;
...
}
public class CombustionCar extends Car {
private Brand brand;
...
}
Avoid inheritance and use composition, but with this I wont be able to use, for example, a List which contain both:
public class ElectricCar {
private Car car;
private Brand brand = Brand.TESLA;//with no setter
...
}
public class CombustionCar {
private Car car;
private Brand brand;
...
}
I'm asking for the most elegant and manteinable solution, I think any of them would be nice to resolve my problem.
Your first solution is incorrect given that you required a non editable BRAND for an electric car.
Your second solution just doesn't work at all excepted if you override both getter and setter of brand field to use your static field, which is not "elegant and mantainable"
Your third solution doesn't make use of object oriented concept.
A simple solution I would use is to let the field brand and its getter in Car superclass, but I'd only define the setter in the CombustionCar class.
Alternatively, if you extend your model, you could create an intermediate abstract superclass "FreeBrandCar" which implements the setter.
Solution with the setter in CombustionCar
abstract public class Car {
protected String brand;
protected Car(final String b) {
this.brand = b;
}
public String getBrand() {
return this.brand;
}
}
public class ElectricCar extends Car {
public ElectricCar() {
super("Tesla");
}
}
public class CombustionCar extends Car {
public CombustionCar(final String b) {
super(b);
}
public void setBrand(final String b) {
this.brand = b;
}
}
Solution with an intermediate class
abstract public class Car {
protected String brand;
protected Car(final String b) {
this.brand = b;
}
public String getBrand() {
return this.brand;
}
}
abstract public class FreeBrandCar extends Car {
public FreeBrandCar (final String b) {
super(b);
}
public void setBrand(final String b) {
this.brand = b;
}
}
public class ElectricCar extends Car {
public ElectricCar() {
super("Tesla");
}
}
public class CombustionCar extends FreeBrandCar {
public CombustionCar(final String b) {
super(b);
}
}
It respects your requirements :
public void test() {
ElectricCar ec = new ElectricCar();
ec.setBrand("..."): // Doesn't compile
CombustionCar cc = new CombustionCar("Ford"); // OK
cc.setBrand("Fiat"); // OK
Arrays.asList(ec, cc)
.stream()
.forEach(car -> System.out.println(car.getBrand())); // prints Tesla and Fiat
}

Vaadin Table - AddNestedContainerProperty

public class LocationBasedRole extends AbstractEntity{
#ManyToMany(fetch=FetchType.LAZY)
private Set<Role> roles=new HashSet<Role>();
#ManyToMany(fetch=FetchType.LAZY)
private Set<Location> locations=new HashSet<Location>();
}
public class Role extends AbstractEntity{
private String name;
}
public class Location extends AbstractEntity{
private String location;
}
I have an entity named locationBasedRole which has 2 properties named roles and locations. Both roles and locations have a #ManyToMany relation with locationBasedRole.
Now I want to have one property of each in a Vaadin Table. It should be something like this,
public class UserForm extends OgsAbstractForm<User>{
MTable<LocationBasedRole> locationBasedRoleTable = new MTable<LocationBasedRole>().withHeight("100%").withWidth("100%");
#Override
protected Component createContent() {
Set<LocationBasedRole> lbRoles=new HashSet<LocationBasedRole>();
roles.addAll(locationBasedRoleFasade.findAll());
BeanItemContainer<LocationBasedRole> bean=new BeanItemContainer<LocationBasedRole>(LocationBasedRole.class);
//It returns an error on the next both lines and I know the reason, but don't know how to solve it.
// If it was no ManyToMany relation and the properties weren't a collection, it would work
bean.addNestedContainerProperty("roles.name");
bean.addNestedContainerProperty("locations.location");
bean.removeContainerProperty("persistent");
bean.removeContainerProperty("id");
bean.addAll(lbRoles);
locationBasedRoleTable.setContainerDataSource(bean);
return new VerticalLayout(locationBasedRoleTable);
}
}
When I remove the properties from the NestedContainerProperties it shows me at least something in the table.
bean.addNestedContainerProperty("roles");
bean.addNestedContainerProperty("locations");
I could use any help!
Thanks in advance!
So if I understand your question right, you want to have the Collections of your BeanItemContainer-Entity displayed in one column each?
I see two possibilities for that.
Option 1 - use a wrapper class for your Sets and use addNestedContainerBean
One possibility would be to not use Sets inside your LocationBasedRole but to use a wrapper class that extends HashSet.
Then you could use the addNestedContainerBean method.
I created a small example with the BeanItemContainer-Entity Team
public class Team {
private String teamName;
private Members teamMembers;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Members getTeamMembers() {
return teamMembers;
}
public void setTeamMembers(Members teamMembers) {
this.teamMembers = teamMembers;
}
}
Which consists of a name and teamMembers. The latter is of type Members:
public class Members extends HashSet<TeamMember> {
public String getMembers() {
return this.stream()
.map(member -> member.getFirstName() + " " + member.getLastName())
.collect(Collectors.joining(","));
}
}
Which is a simple wrapper for the Set that contains instances of TeamMember:
public class TeamMember {
private String firstName;
private String lastName;
private Integer age;
// getters and setters
}
As you can see in the Members class, there is a method getMembers which returns a String, containing a comma separated list of the team members names.
If we now use addNestedContainerBean("teamMembers") Vaadin tries to display all properties contained in the class Members. Vaadin will think getMembers is a getter for a String property called members and so generate a column for it.
Vaadin will also display a column "empty" because it will find the isEmpty method of Set and think empty is a property to display in a column. So we tell Vaadin to remove that column.
The final code of my example looks like:
protected Component createContent() {
Set<Team> teams=new HashSet<>();
for (int teamCounter = 0; teamCounter < 5; teamCounter++) {
Team team = createTeam();
addMembersToTeam(5, team);
teams.add(team);
}
BeanItemContainer<Team> bean=new BeanItemContainer<>(Team.class);
bean.addNestedContainerBean("teamMembers");
bean.removeContainerProperty("teamMembers.empty");
bean.addAll(teams);
teamTable.setContainerDataSource(bean);
return new VerticalLayout(teamTable);
}
The result looks like:
Option 2 - create fake getters and use addNestedContainerProperty
The only thing you have to do for this is extend your BeanItemContainer-Entity (LocationBasedRole) and create a fake getter for each Set you want to be displayed in a column. In your example those two fake getters could be public String getTheRoles() and public String getTheLocations(). Then you can use bean.addNestedContainerProperty("theRoles") and bean.addNestedContainerProperty("theLocations").
In my example my TeamMember class (the counterpart to your Role / Location classes) would still look like in the option above:
public class TeamMember {
private String firstName;
private String lastName;
private Integer age;
// getters and setters
}
And my Team class (your LocationBasedRole) would look like:
public class Team {
private String teamName;
private Set<TeamMember> teamMembers;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Set<TeamMember> getTeamMembers() {
return teamMembers;
}
public void setTeamMembers(Set<TeamMember> teamMembers) {
this.teamMembers = teamMembers;
}
public String getMembers() {
if (teamMembers != null) {
return teamMembers.stream()
.map(member -> member.getFirstName() + " " + member.getLastName())
.collect(Collectors.joining(","));
} else {
return "No members";
}
}
}
Now you can tell vaadin to add the (not existing) property "members" and Vaadin will find the getter getMembers and use this for generating the column. We also have to tell vaadin not to display the original "teamMembers" property. So the final code is:
protected Component createContent() {
Set<Team> teams=new HashSet<>();
for (int teamCounter = 0; teamCounter < 5; teamCounter++) {
Team team = createTeam();
addMembersToTeam(5, team);
teams.add(team);
}
BeanItemContainer<Team> bean=new BeanItemContainer<>(Team.class);
bean.addNestedContainerProperty("members");
bean.removeContainerProperty("teamMembers");
bean.addAll(teams);
teamTable.setContainerDataSource(bean);
return new VerticalLayout(teamTable);
}
and the result looks like:

Is it correct to have static factory method to get a new instance with one field updated?

I think the title is self-descriptive but I will give an example to elaborate on my question. I have a DTO class with few fields (a CarDataTransferObj class in my example). In another class (let's call it class A) I need to create a new instance of that object few times, but with only one field updated (length field in my example). Given DTO must be immutable in class A. As there is "many" fields in the class CarDataTransferObj, I thought about following approach (to avoid repeating code in class A):
#Builder
public class CarDataTransferObj {
private Integer id;
private String color;
private String manufacturer;
private String model;
private String uniqueIdNr;
private Integer nrOfDoors;
private EngineType engineType;
private Integer length;
private Integer safetyLevel;
public static CarDataTransferObj newInstanceWithUpdatedLength(final CarDataTransferObj car, final Integer newLength) {
return CarDataTransferObj.builder()
.id(car.getId())
.color(car.getColor())
.manufacturer(car.getManufacturer())
.model(car.getModel())
.uniqueIdNr(car.getUniqueIdNr())
.nrOfDoors(car.getNrOfDoors())
.engineType(car.getEngineType())
.length(newLength)
.safetyLevel(car.getSafetyLevel())
.build();
}
}
For me it smells like a little anti-pattern usage of static factory methods. I am not sure whether it's acceptable or not, hence the question.
Is using static factory method in the presented way an anti-pattern, and should be avoided ?
In my searching, I didn't come across anyone calling this1 an anti-pattern.
However, it is clear that if you try to do this using a classic builder that is not specifically implemented to support this mode of operation .... it won't work. For instance, the example CarBuilderImpl in the Wikipedia article on the Builder design pattern puts the state into an eagerly created Car instance. The build() method simply returns that object. If you tried to reuse that builder in the way that you propose, you would end up modifying a Car that has already been built.
There is another problem you would need to worry about. In we modified the Wikipedia CarBuilder example to add actual wheels (rather than a number of wheels) to the Car being built, we have to worry about creating cars that share the same wheels.
You could address these things in a builder implementation, but it is unclear whether the benefits out-weigh the costs.
If you then transfer this thinking to doing this using a factory method, you come to a slightly different conclusion.
If you are doing this as a "one-off", that's probably OK. You have a specific need, the code is clunky ... but so is the problem.
If you needed to do this for lots of different parameters, or combinations of parameters, this is not going to scale.
If the objects that are created are mutable, then this approach is could be problematic in a multi-threaded environment depending on how you control access to the objects you are using as templates.
1 - There are no clear measurable criteria for whether something is an anti-pattern or not. It is a matter of opinion. Admittedly, for many anti-patterns, there will be wide-scale agreement on that opinion.
It seems a little inefficient to construct an entirely new instance via a builder every time you want to make a new copy with a small modification. More significantly, it sounds like the places where you need the class to be immutable are isolated to places like class A. Why not try something like this:
public interface ICarDataTransferObject {
public Integer GetId();
public String GetColor();
public String GetManufacturer();
public String GetModel();
public String GetUUID();
public Integer GetDoorCount();
public EngineType GetEngineType();
public Integer GetLength();
public Integer GetSafteyLevel();
}
public class CarDataTransferObject Implements ICarDataTransferObject {
private Integer _id;
private String _color;
private String _manufacturer;
private String _model;
private String _uniqueIdNr;
private Integer _nrOfDoors;
private EngineType _engineType;
private Integer _length;
private Integer _safetyLevel;
public Integer GetId() { return _id; }
public void SetId(Integer id) { _id = id; }
public String GetColor() { return _color; }
public void SetColor(String color) { _color = color; }
public String GetManufacturer() { return _manufacturer; }
public void SetManufacturer(String manufacturer) { _manufacturer = manufacturer; }
public String GetModel() { return _model; }
public void SetModel(String model) { _model = model; }
public String GetUUID() { return _uniqueIdNr; }
public void SetUUID(String uuid) { _uniqueIdNr = uuid; }
public Integer GetDoorCount() { return _nrOfDoors; }
public void SetDoorCount(Integer count) { _nrOfDoors = count; }
public EngineType GetEngineType() { return _engineType; }
public void SetEngineType(EngineType et) { _engineType = et; }
public Integer GetLength() { return _length; }
public void SetLength(Integer length) { _length = length; }
public Integer GetSafteyLevel() { return _safetyLevel; }
public void SetSafteyLevel(Integer level) { _safteyLevel = level; }
public CarDataTransferObject() {}
public CarDataTransferObject(ICarDataTransferObject other) { ... }
public ReadOnlyCarDataTransferObject AsReadOnly() {
return ReadOnlyCarDataTransferObject (this);
}
}
}
public class ReadOnlyCarDataTransferObject Implements ICarDataTransferObject {
private ICarDataTransferObject _dto = null;
public Integer GetId() { return _dto.GetId(); }
public String GetColor() { return _dto.GetColor(); }
public String GetManufacturer() { return _dto.GetManufacturer(); }
public String GetModel() { return _dto.GetModel(); }
public String GetUUID() { return _dto.GetUUID(); }
public Integer GetDoorCount() { return _dto.GetDoorCount(); }
public EngineType GetEngineType() { return _dto.GetEngineType(); }
public Integer GetLength() { return _dto.GetLength(); }
public Integer GetSafteyLevel() { return _dto.GetSafteyLevel; }
public ReadOnlyCarDataTransferObject (ICarDataTransferObject other) {
_dto = other;
}
}
Now when you want class A to have a copy no one can modify, just use the copy constructor and only expose a ReadOnly version of that copy.
public class A {
ICarDataTransferObject _dto;
ReadOnlyCarDataTransferObject _readOnlyDTO;
public ICarDataTransferObject GetDTO() { return _readOnlyDTO; }
public A(ICarDataTransferObject dto) {
_dto = new CarDataTransferObject(dto);
_readOnlyDTO = new ReadOnlyCarDataTransferObject(_dto);
}
}
You commonly see this approach in .NET applications.
While it is debatable whether your static method is an anti-pattern or not, it surely won't scale for combinations of different attributes. Nonetheless, even if it's not an anti-pattern, I think there is a better way to accomplish what you need.
There's a variant of the traditional builder pattern that, instead of creating a new empty builder, accepts an already built object and creates an already initialized builder. Once you create the builder this way, you simply change the length attribute in the builder. Finally, build the object. In plain code (no Lombok, sorry) it could be like this:
public class CarDataTransferObj {
private Integer id;
private String color;
// other attributes omitted for brevity
private Integer length;
// Private constructor for builder
private CarDataTransferObj(Builder builder) {
this.id = builder.id;
this.color = builder.color;
this.length = builder.length;
}
// Traditional factory method to create and return builder
public static Builder builder() {
return new Builder();
}
// Factory method to create and return builder initialized from an instance
public static Builder builder(CarDataTransferObj car) {
Builder builder = builder();
builder.id = car.id;
builder.color = car.color;
builder.length = car.length;
return builder;
}
// getters
public static class Builder {
private Integer id;
private String color;
private Integer length;
private Builder() { }
public Builder withId(Integer id) { this.id = id; return this; }
public Builder withColor(String color) { this.color = color; return this; }
public Builder withLength(Integer length) { this.length = length; return this; }
public CarDataTransferObj build() {
return new CarDataTransferObj(this);
}
}
}
Now with all this infrastructure in place, you can do what you want as easy as:
CarDataTransferObj originalCar = ... // get the original car from somewhere
CarDataTransferObj newCar = CarDataTransferObj.builder(originalCar)
.withLength(newLength)
.build();
This approach has the advantage that it scales well (it can be used to change any combination of parameters). Maybe all this builder's code seems boilerplate, but I use an IntelliJ plugin to create the builder with two keystrokes (including the variant factory method that accepts a built instance to create an initialized builder).
I'm still new to java but..
I guess making a copy method which takes the CarDataTransferObj object variables and sets their values to another CarDataTransferObj object variables and changing the the length using it's setter method would be better idea
Example:
public class CarDataTransferObj {
private Integer id;
private String color;
private String manufacturer;
private String model;
private String uniqueIdNr;
private Integer nrOfDoors;
private EngineType engineType;
private Integer length;
private Integer safetyLevel;
public void Copy(CarDataTransferObj copy) { //Could add another parameter here to be the new length
copy.setId(id);
copy.set(color);
copy.setManufacturer(manufacturer);
copy.setModel(model);
copy.setUniqueIdNr(uniqueIdNr));
copy.setNrOfDoors(nrOfDoors));
copy.setEngineType(engineType));
copy.setLength(length);
copy.setSafetyLevel(safetyLevel));
}
}
public class SomeOtherClass {
CarDataTransferObj car1 = new CarDataTransferObj(); //Using this way made you able to use the constructor for a more useful thing
//You set the variables you want for car1 here
CarDataTransferObj car2 = new CarDataTransferObj();
car1.Copy(car2)
car2.setLength(newLength) //Set the new length here
}

Copying nested objects with references to each other

I have two tables in database having one to many relationship.
while I fetch the table User, I want to copy the data in the User (with the data related to Vehicle) to another object UserDuplicate (and VehicleDuplicate).
I tried using BeanUtils.copyProperties but the nested references still refer to old object.
I want to know what is the way to copy the nested objects.
Thanks.
import java.util.Set;
public class User {
private Set<Vehicle> vehs = new HasHSet();
public Set<Vehicle> getVehs() {
return vehs;
}
public void setVehs(Set<Vehicle> vehs) {
this.vehs = vehs;
}
}
class Vehicle {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
class UserDuplicate {
private Set<VehicleDuplicate> vehDup=new HasHSet();
public Set<VehicleDuplicate> getVehDup() {
return vehDup;
}
public void setVehDup(Set<VehicleDuplicate> vehDup) {
this.vehDup = vehDup;
}
}
class VehicleDuplicate {
private UserDuplicate userDup;
public UserDuplicate getUserDup() {
return userDup;
}
public void setUserDup(UserDuplicate userDup) {
this.userDup = userDup;
}
}
I like to use copy constructors in these cases:
class UserDuplicate {
...
UserDuplicate(User user) {
...
if (user.getVehs() != null) {
vehDup = new HashSet<>();
for (Vehicle v: user.getVehs()) {
vehDup.add(new VehicleDuplicate(this, v));
}
}
}
...
class VehicleDuplicate {
...
VehicleDuplicate(UserDuplicate userDup, Vehicle veh) {
this.userDup = userDup;
...
}
One approach could be to use a mapper to copy what you need to the duplicated object, this approach can have a lighter footprint on your code...
You can for example use jackson-databind
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
UserDuplicate duplicated = mapper.readValue(mapper.writeValueAsString(source), UserDuplicate.class);
// set the correct duplicate user in the duplicated vehicule
duplicated.getVehDup().forEach(x -> x.setUserDup(duplicated));
This will serialize the source object into JSON and then deserialize it into an instance of the duplicated object.
Because you are saying that your schemas are more or less the same you can take care of the annotations provided by Jackson for example to ignore some field.
static class User {
#JsonIgnore // Allows to ignore attributes from stadging to production
private String iDontWantToCopyThis = "blablabla";
private Set<Vehicle> vehs;
public Set<Vehicle> getVehs() {
return vehs;
}
public void setVehs(Set<Vehicle> vehs) {
this.vehs = vehs;
}
}
Doing this the iDontWantToCopyThis field won't be copied into the duplicated object.
Because your Vehicle contains a reference to the user you need to annotate with #JsonIgnore to avoid the recursivity during the deserialization.
static class Vehicle {
#JsonIgnore
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
And because the name of the vehicule set is different into the UserDuplicate class you must use the annotation #JsonProperty("vehs") to let the mapper know how to match the datas.
static class UserDuplicate {
#JsonProperty("vehs") // need to specify the source name into the json used to load the user duplicated
private Set<VehicleDuplicate> vehDup;
public Set<VehicleDuplicate> getVehDup() {
return vehDup;
}
public void setVehDup(Set<VehicleDuplicate> vehDup) {
this.vehDup = vehDup;
}
}
If you have data transformations too complex to be processed just by the annotations you can also create custom serializer or deserializer...

How can I implement these 2 simple queries using Spring data JPA with the "query creation from method names" strategy?

I am pretty new in Spring Data and I have to write what in the official documentation seems to be called Query creation from method names, here the reference:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
As you can see in the previous example show the creation of a query by the definition of a method name, for example:
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
that I think return a list of Person object that have a specific email affress and a specific lastname.
So I am trying to do the same thing in my project that use Hibernate as JPA provider.
In my project I have this Twb1012Regione entity class that map the anagrafiche.TWB1012_REGIONE on the database:
#Entity
#Table(name="anagrafiche.TWB1012_REGIONE")
#NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="COD_REG")
private String codReg;
#Column(name="COD_ARE_GEO")
private String codAreGeo;
#Column(name="COD_CIT")
private String codCit;
#Column(name="COD_IST")
private int codIst;
#Column(name="COD_PGM_ULT_MOV")
private String codPgmUltMov;
#Column(name="COD_UTE_ULT_MOV")
private String codUteUltMov;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="DAT_ORA_ULT_MOV")
private Date datOraUltMov;
#Column(name="DES_REG")
private String desReg;
//bi-directional many-to-one association to Tpg1029Provnuoist
#OneToMany(mappedBy="twb1012Regione")
private List<Tpg1029Provnuoist> tpg1029Provnuoists;
//bi-directional many-to-one association to Twb1013Provincia
#OneToMany(mappedBy="twb1012Regione")
private List<Twb1013Provincia> twb1013Provincias;
public Twb1012Regione() {
}
public String getCodReg() {
return this.codReg;
}
public void setCodReg(String codReg) {
this.codReg = codReg;
}
public String getCodAreGeo() {
return this.codAreGeo;
}
public void setCodAreGeo(String codAreGeo) {
this.codAreGeo = codAreGeo;
}
public String getCodCit() {
return this.codCit;
}
public void setCodCit(String codCit) {
this.codCit = codCit;
}
public int getCodIst() {
return this.codIst;
}
public void setCodIst(int codIst) {
this.codIst = codIst;
}
public String getCodPgmUltMov() {
return this.codPgmUltMov;
}
public void setCodPgmUltMov(String codPgmUltMov) {
this.codPgmUltMov = codPgmUltMov;
}
public String getCodUteUltMov() {
return this.codUteUltMov;
}
public void setCodUteUltMov(String codUteUltMov) {
this.codUteUltMov = codUteUltMov;
}
public Date getDatOraUltMov() {
return this.datOraUltMov;
}
public void setDatOraUltMov(Date datOraUltMov) {
this.datOraUltMov = datOraUltMov;
}
public String getDesReg() {
return this.desReg;
}
public void setDesReg(String desReg) {
this.desReg = desReg;
}
public List<Tpg1029Provnuoist> getTpg1029Provnuoists() {
return this.tpg1029Provnuoists;
}
public void setTpg1029Provnuoists(List<Tpg1029Provnuoist> tpg1029Provnuoists) {
this.tpg1029Provnuoists = tpg1029Provnuoists;
}
public Tpg1029Provnuoist addTpg1029Provnuoist(Tpg1029Provnuoist tpg1029Provnuoist) {
getTpg1029Provnuoists().add(tpg1029Provnuoist);
tpg1029Provnuoist.setTwb1012Regione(this);
return tpg1029Provnuoist;
}
public Tpg1029Provnuoist removeTpg1029Provnuoist(Tpg1029Provnuoist tpg1029Provnuoist) {
getTpg1029Provnuoists().remove(tpg1029Provnuoist);
tpg1029Provnuoist.setTwb1012Regione(null);
return tpg1029Provnuoist;
}
public List<Twb1013Provincia> getTwb1013Provincias() {
return this.twb1013Provincias;
}
public void setTwb1013Provincias(List<Twb1013Provincia> twb1013Provincias) {
this.twb1013Provincias = twb1013Provincias;
}
public Twb1013Provincia addTwb1013Provincia(Twb1013Provincia twb1013Provincia) {
getTwb1013Provincias().add(twb1013Provincia);
twb1013Provincia.setTwb1012Regione(this);
return twb1013Provincia;
}
public Twb1013Provincia removeTwb1013Provincia(Twb1013Provincia twb1013Provincia) {
getTwb1013Provincias().remove(twb1013Provincia);
twb1013Provincia.setTwb1012Regione(null);
return twb1013Provincia;
}
}
So, into my project I have defined a Twb1012RegioneRepository interface that is my repository class defined on the previous Twb1012Regione entity class:
#RepositoryDefinition(domainClass=Twb1012Regione.class, idClass=String.class)
public interface Twb1012RegioneRepository extends JpaRepository<Twb1012Regione, String> {
// I have to implement it
}
Now my problem is that I want to create 2 methods (that implement 2 queries by method name as described by the previous tutorial) that perform the following tasks:
1) Return the list of all the Twb1012Regione representing all the record of the TWB1012_REGIONE table on the DB.
2) Given a specific id (the value of the String codReg field, PK of the Twb1012Regione class) I want to obtain the Twb1012Regione object associated to this record.
How can I implement these queries? I have some difficulties to do it
Tnx
You don't need to implement the methods. The Spring Data Repository API will construct query for you as the JpaRepository already has following methods:
List findAll(Iterable ids)
T getOne(ID id)
That's the whole point with the Spring Data Repository - To reduce the boiler plate code that you write.

Categories