Call length and specific items of list inside a list - java

Please view image for the scenario I want to describe.
I want to have a list inside a list and know what is the size of that list (inside) and how to call a particular data from that list.
--also please include how to add the hobbyList into candidateList
So let's say I want to call the size of hobbyList for the 2nd Candidate something like <code>CandidateList(1).HobbyList.length()</code>? or .size()?
What if I want to call the value of the 3rd hobby of the 2nd candidate
CandidateList(1).HobbyList(2)?
I don't know if I'm already clear with what I want so please don't hesitate to ask me.
Thank you! :)

Hobby Class
public class Hobby {
private int id;
private String hobby;
public Hobby(int id, String hobby) {
this.id = id;
this.hobby = hobby;
}
// Getters and setters
}
Candidate Class
import java.util.List;
public class Candidate {
private int id;
private String candidateName;
private List<Hobby> hobbies;
public Candidate(int id, String candidateName, List<Hobby> hobbies) {
this.id = id;
this.candidateName = candidateName;
this.hobbies = hobbies;
}
// Getters and setters
}
MainClassDemo having main method
import java.util.ArrayList;
import java.util.List;
public class MainClassDemo {
public static void main(String[] args) {
Hobby c1Hb1 = new Hobby(1, "Cricket");
Hobby c1Hb2 = new Hobby(2, "Hockey");
Hobby c1Hb3 = new Hobby(3, "Football");
List<Hobby> hobbies1 = new ArrayList<Hobby>();
hobbies1.add(c1Hb1);
hobbies1.add(c1Hb2);
hobbies1.add(c1Hb3);
Candidate candidate1 = new Candidate(1, "Ankit", hobbies1);
Hobby c2Hb1 = new Hobby(4, "FB");
Hobby c2Hb2 = new Hobby(5, "TW");
Hobby c2Hb3 = new Hobby(6, "INSTA");
Hobby c2Hb4 = new Hobby(6, "Pininterest");
Hobby c2Hb5 = new Hobby(6, "Quara");
List<Hobby> hobbies2 = new ArrayList<Hobby>();
hobbies2.add(c2Hb1);
hobbies2.add(c2Hb2);
hobbies2.add(c2Hb3);
hobbies2.add(c2Hb4);
hobbies2.add(c2Hb5);
Candidate candidate2 = new Candidate(2, "Bhumi", hobbies2);
List<Candidate> candidateList = new ArrayList<Candidate>();
candidateList.add(candidate1);
candidateList.add(candidate2);
System.out.println("No of Candidates:->" + candidateList.size());
System.out.println("Hobby list size for the 1st Candidate:->" + candidateList.get(0).getHobbies().size());
System.out.println("Hobby list size for the 2nd Candidate:->" + candidateList.get(1).getHobbies().size());
System.out.println("1st Hobby of 1st Candidate:->" + candidateList.get(0).getHobbies().get(0).getHobby());
System.out.println("3rd Hobby of 2nd Candidate:->" + candidateList.get(1).getHobbies().get(2).getHobby());
}
}
Output
No of Candidates:->2
Hobby list size for the 1st Candidate:->3
Hobby list size for the 2nd Candidate:->5
1st Hobby of 1st Candidate:->Cricket
3rd Hobby of 2nd Candidate:->INSTA

I'm not sure I understand your question fully.
Assuming you have a class Candidate and an class Hobby:
public class Candidate {
public final List<Hobby> hobbyList = new ArrayList<>();
...
}
public class Hobby {
...
}
And an list of candidates:
List<Candidate> candidateList;
You would get the number of hobbies of the second candidate in the list by calling with the expression:
candidateList.get(1).hobbyList.size()
and you would get the 3rd hobby in the hobby list of the 2nd candidate with:
candidateList.get(1).hobbyList.get(2)
to add a hobby to the second candidate, you would do:
candidateList.get(1).hobbyList.add(hobby);
Now I like to encapsulate the fields of a class, at least when they can be modified, so I would make hobbyList private, and add a few methods:
public class Candidate {
private final List<Hobby> hobbyList = new ArrayList<>();
public int getHobbyCount() {
return hobbyList.size();
}
public Hobby getHobby(int i) {
return hobbyList.get(i);
}
public List<Hobby> getHobbies() {
return new ArrayList<>(hobbyList);
}
public void addHobby(Hobby hobby) {
hobbyList.add(hobby);
}
public void removeHobby(Hobby hobby) {
hobbyList.remove(hobby);
}
public void removeAllHobbies() {
hobbyList.clear();
}
...
}
So you now can write things like:
candidateList.get(1).getHobbyCount();
candidateList.get(1).getHobby(2);
candidateList.get(1).add(hobby);
etc...

Related

Java8 stream: Iterate over nested list

Trying to get my head round the Java 8 streams syntax with a simple example. Had a look at the other similar questions on this topic, but could not find any solutions that would match my example and would work for me.
I have a class as follow
import java.util.List;
public class Car {
private String model;
private String make;
private String carName;
private List<Specification> specification;
public Car(String model, String make, String carName, List<Specification> specification) {
this.model = model;
this.make = make;
this.carName = carName;
this.specification = specification;
}
public String getModel() {
return model;
}
public String getMake() {
return make;
}
public String getCarName() {
return carName;
}
public List<Specification> getSpecification() {
return specification;
}
}
public class Specification {
private String name;
private String value;
public String getName() {
return name;
}
public String getValue() {
return value;
}
public Specification(String name, String value) {
this.name = name;
this.value = value;
}
}
And I have the main method
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RegisterCar {
public static void main(String[] args) throws IOException {
List<String> carNames = new ArrayList<>();
carNames.add("Audi");
carNames.add("BMW");
carNames.add("Toyota");
List<String> colour = new ArrayList<>();
colour.add("red");
colour.add("white");
}
}
I want to create a list of car object with each item of carNames. But if the car name is 'Audi' or 'BMW' it should create only one object of the class as follow
List<Car> carList = new ArrayList<>();
Specification musicSystem = new Specification("MusicSysten" , "present");
List<Specification> specList= new ArrayList<>();
specList.add(musicSystem);
carList.add(new Car("Q5", "2020", "Audi", specList));
carList.add(new Car("X2", "2020", "BMW", specList));
But if the item is 'Toyota' then it should create two object each for each color.
List<Specification> specListRed= new ArrayList<>();
specListRed.add(musicSystem);
specListRed.add(redColor);
List<Specification> specListWhite= new ArrayList<>();
specListWhite.add(musicSystem);
specListWhite.add(whiteColor);
carList.add(new Car("Camry", "2020", "Toyota", specListRed));
carList.add(new Car("Camry", "2020", "Toyota", specListWhite));
I am trying to write a common method for creating object and adding to a list. I have tried something like this, but I won't create correct two object for Toyota.
carList = carNames.stream()
.map(carName -> new Car(model, make, carName, specList))
.collect(Collectors.toList());
please suggest how can I achieve this with streams.
If you want the name "Toyota" to be used twice it should be contained in the list of names twice, since the lambda in map() is executed for every name.
But inside your map-Call: Where do you get and model & make?
First thing you need to understand is that map operation always preserves same amount (i.e. cardinality) as your input.
If you will pass 3 items - no matter which function you have, result will always be 3.
Next thing worth looking into: flatMap function. This function can transform any amount of inputs to any amount of results! This looks like what we need.
var carList = carNames.stream()
.flatMap(carName -> {
if (carName.equals("Toyota") {
return Stream.of(new Car(model, make, carName, specList1),
new Car(model, make, carName, specList2));
} else {
return Stream.of(new Car(model, make, carName, specList));
})
.collect(Collectors.toList());
Since it's all about creation and holding of object. Foucus on equals() and and hashcode() methods.
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Car)) {
return false;
}
Car car = (Car) o;
if (carName.equals("BMW")) {
return true;
}
if (carName.equals("Toyota")) {
return false;
}
return Objects.equals(carName , car.carName);
}
#Override
public int hashCode() {
return 1;
}
Driver methood
List<String> carNames = new ArrayList<>();
carNames.add("Audi");
carNames.add("BMW");
carNames.add("Toyota");
carNames.add("Toyota");
List<String> colours = new ArrayList<>();
colours.add("red");
colours.add("white");
Set carList = carNames.stream()
.map(carName -> {
Set ss = new HashSet();
if (carName.equals("Toyota")) {
ss.addAll(colours.stream().map(colr -> new Car("model" , "make" , carName , null)).collect(Collectors.toSet()));
} else {
ss.add(new Car("model" , "make" , carName , null));
}
return ss;
})
.collect(HashSet::new, Set::addAll, Set::addAll);

How to override the #AdminPresentation for existing attributes [Broadleaf Commerce]

I am trying to override the #AdminPresentation of the following attribute defined in ProductImpl:
#Column(name = "DISPLAY_TEMPLATE")
#AdminPresentation(friendlyName = "ProductImpl_Product_Display_Template",
group = GroupName.Advanced)
protected String displayTemplate;
Currently, it is displayed as a text field by default as there is no fieldType attribute provided. But I want to display a dropdown select menu with predefined values such as Product and Plan. Here is what I've tried so far:
I've created a class DisplayTemplateType that implements BroadleafEnumerationType and defined PLAN and PRODUCT enums. Here is the code of that class:
public class DisplayTemplateType implements Serializable, BroadleafEnumerationType {
private static final long serialVersionUID = 7761108654549553693L;
private static final Map<String, DisplayTemplateType> TYPES = new LinkedHashMap<String, DisplayTemplateType>();
public static final DisplayTemplateType PLAN = new DisplayTemplateType("PLAN", "PLAN");
public static final DisplayTemplateType PRODUCT = new DisplayTemplateType("PRODUCT", "PRODUCT");
public static DisplayTemplateType getInstance(final String type) {
return TYPES.get(type);
}
private String type;
private String friendlyType;
public DisplayTemplateType() {
//do nothing
}
public DisplayTemplateType(final String type, final String friendlyType) {
this.friendlyType = friendlyType;
setType(type);
}
#Override
public String getType() {
return type;
}
#Override
public String getFriendlyType() {
return friendlyType;
}
private void setType(final String type) {
this.type = type;
if (!TYPES.containsKey(type)) {
TYPES.put(type, this);
} else {
throw new RuntimeException("Cannot add the type: (" + type + "). It already exists as a type via " + getInstance(type).getClass().getName());
}
}
// equals() and hashCode() implementation is removed for readability
}
Then in applicationContext-admin.xml file, I have added the following override properties:
<mo:override id="blMetadataOverrides">
<mo:overrideItem ceilingEntity="org.broadleafcommerce.core.catalog.domain.Product">
<mo:field name="displayTemplate">
<mo:property name="explicitFieldType" value="BROADLEAF_ENUMERATION"/>
<mo:property name="broadleafEnumeration" value="com.community.core.domain.DisplayTemplateType"/>
</mo:field>
</mo:overrideItem>
</mo:override>
But it didn't change anything. Am I missing something here?
Finally, after trying many things, I came up with a workaround. Instead of going with the XML based approach, I had to extend the ProductImpl class to override #AdminPresentation of its attributes. But for extending I needed to define an #Entity and as a result, I needed to create a useless table to bind to that entity. I know this is not the perfect approach but I couldn't find any better solution for this. Here is my code, so that someone might get help from it in the future:
#Entity
#Immutable
#AdminPresentationMergeOverrides({
#AdminPresentationMergeOverride(name = "displayTemplate", mergeEntries = {
#AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.FIELDTYPE, overrideValue = "BROADLEAF_ENUMERATION"),
#AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.BROADLEAFENUMERATION, overrideValue = "com.community.core.domain.DisplayTemplateType"),
#AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.REQUIREDOVERRIDE, overrideValue = "REQUIRED"),
#AdminPresentationMergeEntry(propertyType = PropertyType.AdminPresentation.DEFAULTVALUE, overrideValue = "PLAN")
})
})
public class CustomProduct extends ProductImpl {
private static final long serialVersionUID = -5745207984235258075L;
}
This is how it is displayed now:

How to collect properties of List<Map> by unique property using MultiMap?

I have List of stories. Using unique property(id) I want to collect keyword and targeting as list of values. Can I do this with MultiMap? Or is there other library for this?
[{
id = 1,
title = Onboarding,
keyword = new joinee,
targeting = finance
}, {
id = 1,
title = Onboarding,
keyword = training,
targeting = HR
}]
The Desired output must like this :
{
id = 1,
title = Onboarding,
keyword = [new joinee,training], //may be keywords - plural
targeting = [HR,finance]
}
Sample my tried Code as follows:
package prac;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaPrac {
public static void main(String[] args) {
Multimap<Integer, Map> multiMap = ArrayListMultimap.create();
List<Map> stories=new ArrayList();
Map story1=new HashMap();
story1.put("id", 1);
story1.put("title", "Onboarding");
story1.put("keyword","new joinee");
story1.put("targeting","finance");
Map story2=new HashMap();
story2.put("id", 1);
story2.put("title", "Onboarding");
story2.put("keyword","training");
story2.put("targeting","HR");
stories.add(story1);
stories.add(story2);
System.out.println(stories);
stories.forEach((story) -> {
multiMap.put((Integer) story.get("id"), story);
});
}
}
A multimap can only store multiple values per key but what you want is to combine those multiple values so that you get one element that has the same id and title as well as a collection of keywords and targeting information. Thus it would probably be best to either have something like MultiStory or already have Story contain those collections.
I'd suggest using proper objects instead of just maps but with maps and Java 8 lambdas you could use compute() etc. to build maps that contain collections and combine maps that don't.
Here's an example of how you'd do it with maps. Note that this is very bad style and an example using proper pojos will follow:
Disclaimer: example based on the OP's code, not recommended (read text above)
//Problem 1: we don't know the type of the values, i.e. we could put anything for "id" etc.
Map<String, Object> story1=new HashMap<>();
story1.put("id", 1);
story1.put("title", "Onboarding");
story1.put("keyword","new joinee");
story1.put("targeting","finance");
Map<String, Object> story2=new HashMap<>();
story2.put("id", 1);
story2.put("title", "Onboarding");
story2.put("keyword","training");
story2.put("targeting","HR");
List<Map<String, Object>> stories=new ArrayList<>();
stories.add(story1);
stories.add(story2);
Map<Integer, Map<String, Object>> combined = new HashMap<>();
stories.forEach((story) -> {
//Problem 2: because we don't know the type of the values we need a lot of nasty casts
Map<String, Object> combinedStory = combined.computeIfAbsent( (Integer)story.get( "id" ), k -> new HashMap<String, Object>() );
combinedStory.put("id", story.get( "id" ) );
combinedStory.put("title", story.get( "title" ) );
//Problem 3: the combined map would look a lot like your "story" maps but would contain different types
((List<String>)combinedStory.computeIfAbsent( "keyword", v -> new List<String>() )).add( (String)story.get("keyword") );
((List<String>)combinedStory.computeIfAbsent( "targeting", v -> new List<String>() )).add( (String)story.get("targeting") );
});
Using POJOs
Here's a greatly simplified example of how you'd do it with proper Java objects (POJOs). Note that those are meant to resemble your code as much as possible and there are a lot of other issues but addressing those would be way too much here and better designed code would be a lot larger and probably harder to understand - after all it's just meant to show you a difference.
First let's define our classes (for simplicity I made the fields public, you'd normally not do that):
class Story {
public final int id;
public String title;
public String keyword;
public String targeting;
public Story(int storyId) {
id = storyId ;
}
}
class MultiStory {
public final int id;
public String title;
public Set<String> keywords = new HashSet<>();
public Set<String> targetingInfo = new HashSet<>();
public MultiStory( int storyId ) {
id = storyId ;
}
}
Then let's reiterate the code above:
Story story1=new Story( 1 );
story1.title = "Onboarding";
story1.keyword = "new joinee";
story1.targeting = "finance";
Story story2=new Story( 1 );
story2.title = "Onboarding";
story2.keyword = "training";
story2.targeting = "HR";
List<Story> stories=new ArrayList<>();
stories.add(story1);
stories.add(story2);
Map<Integer, MultiStory> combined = new HashMap<>();
stories.forEach((story) -> {
MultiStory multiStory = combined.computeIfAbsent( story.id, v -> new MultiStory( story.id ) );
multiStory.title = story.title;
multiStory.keywords.add( story.keyword );
multiStory.targetingInfo.add( story.targeting );
});
As you can see, there are no casts needed and it's clear what fields are available (though not necessarily filled) which makes it easier to reason about the code and spot errors (the compiler can help a lot here which it couldn't to in the example that uses maps).
Here is a solution using classes to represent the story and tags:
public static void main(String[] args) {
TagsCollector app = new TagsCollector();
app.go();
}
private void go() {
List<Story> stories = createStories();
System.out.println(stories);
Map<Long, Tags> tagsById = collectTags(stories);
tagsById.forEach((aLong, tags) -> System.out.println(tags));
}
private List<Story> createStories() {
return Arrays.asList(
new Story(1, "Onboarding", "new joinee", "finance"),
new Story(1, "Onboarding", "training", "HR")
);
}
private Map<Long, Tags> collectTags(List<Story> stories) {
Map<Long, Tags> tagsById = new HashMap<>();
stories.forEach(s -> {
Tags tags = tagsById.computeIfAbsent(s.id, v -> new Tags(s));
tags.getKeywords().add(s.getKeyword());
tags.getTargetings().add(s.getTargeting());
});
return tagsById;
}
Class used to represent the Story:
public class Story {
private final long id;
private final String title;
private final String keyword;
private final String targeting;
public Story(long id, String title, String keyword, String targeting) {
this.id = id;
this.title = title;
this.keyword = keyword;
this.targeting = targeting;
}
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getKeyword() {
return keyword;
}
public String getTargeting() {
return targeting;
}
#Override
public String toString() {
return String.format("Story %s, title=%s, keyword=%s, targeting=%s", id, title, keyword, targeting);
}
}
Class used to represent the Tags:
public class Tags {
private final long id;
private final String title;
private final List<String> keywords = new ArrayList<>();
private final List<String> targetings = new ArrayList<>();
Tags(Story story) {
this.id = story.id;
this.title = story.title;
}
public List<String> getKeywords() {
return keywords;
}
public List<String> getTargetings() {
return targetings;
}
#Override
public String toString() {
return String.format("Tags for id %s, title:%s: keywords=%s, targetings=%s", id, title, keywords, targetings);
}
}
Output
[Story 1, title=Onboarding, keyword=new joinee, targeting=finance, Story 1, title=Onboarding, keyword=training, targeting=HR]
Tags for id 1, title:Onboarding: keywords=[new joinee, training], targetings=[finance, HR]
Yes, you can do that with a Multimap. First I would define a pojo for Story in order to make things clearer:
public class Story {
private int id;
private String title;
private String keyword;
private String targeting;
//getters setters
}
Second you need to define a key with hashcode and equals.
public static class StoryKey {
private final int id;
private final String title;
public StoryKey(int id, String title) {
this.id = id;
this.title = title;
}
//getters
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StoryKey storyKey = (StoryKey) o;
if (id != storyKey.id) return false;
return title != null ? title.equals(storyKey.title) : storyKey.title == null;
}
#Override
public int hashCode() {
int result = id;
result = 31 * result + (title != null ? title.hashCode() : 0);
return result;
}
The code will look like:
ArrayListMultimap<StoryKey, Story> multiMap = ArrayListMultimap.create();
List<Story> stories = new ArrayList();
Story story1 = new Story();
story1.setId(1);
story1.setTitle("Onboarding");
story1.setKeyword("training");
story1.setTargeting("HR");
Story story2 = new Story();
story2.setId(1);
story2.setTitle("Onboarding");
story2.setKeyword("new joinee,");
story2.setTargeting("finance");
stories.add(story1);
stories.add(story2);
System.out.println(stories);
stories.
forEach((story) -> {
multiMap.put(new StoryKey(story.getId(), story.getTitle()), story);
});
multiMap.keys().forEach(key ->
System.out.println(
"id =" + key.getId() +
" title =" + key.getTitle()+
"keyword =" + multiMap.get(key).stream().map(story->story.getKeyword()).collect(Collectors.toList()).toString()+
"targeting ="+ multiMap.get(key).stream().map(story->story.getTargeting()).collect(Collectors.toList()).toString())
);

Is deep copy necessary on passing an ArrayList to a copy constructor?

I'm trying to figure out the best way to implement the code I'm working on.
I have two options of how to pass the data to my constructor.
First way
private String ISBN;
private String title;
private ArrayList <Person>authors = new ArrayList<>();
private ArrayList <BookCategory>subjectCategories = new ArrayList<>();
public Book (String isbn, String title,
ArrayList <Person>authors, ArrayList <BookCategory>categories) {
//call the checkISBN method
boolean check = checkISBN(isbn);
if (check ==true) {
this.ISBN= isbn;
}
else {
throw new IllegalArgumentException("Invalid ISBN");
}
this.title = title;
for(int index =0; index<authors.size(); index++) {
this.authors.add(authors.get(index));
}
}
Second way
private String ISBN;
private String title;
private ArrayList <Person>authors = new ArrayList<>();
private ArrayList <BookCategory>subjectCategories = new ArrayList<>();
public Book (String isbn, String title,
ArrayList <Person>authors, ArrayList <BookCategory>categories) {
//call the checkISBN method
boolean check = checkISBN(isbn);
if (check ==true) {
this.ISBN= isbn;
}
else {
throw new IllegalArgumentException("Invalid ISBN");
}
this.title = title;
this.authors = authors;
}
Does it make a difference?
I'm not sure because it seems like it would be unnecessary to declare the Book with a copy of the Authors ArrayList instead of the original ArrayList.
What is the correct way to do this, and why?
If you copy, you spend some time to gain security. If you didn't copy, then a client could construct a Book with some authors and later change the authors which will be reflected in the Book:
List<Person> authors = new ArrayList<>();
authors.add(new Person("J.K. Rowling"));
Book harryPotter = new Book("some ISBN", "Harry Potter", authors, new ArrayList<>());
// Harry Potter is written by J.K. Rowling.
authors.set(1, new Person("pkpnd"));
// Harry Potter is now written by pkpnd??
If that sounds bad in your use case, you should make a copy.
Note that you aren't performing a deep copy, only a shallow copy. You'll need to create entirely new Person objects when copying:
this.authors = new ArrayList<>();
for (Person p : authors) {
this.authors.add(new Person(p.getName()));
}
This is an example of how to make a deep copy of your class with serialization. This ensures everything is a brand new reference:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
public Person(String name) {this.name = name;}
public String setName(String name) {return this.name = name;}
/**
* make a deep copy with serialization without creating files
* convert class object to a stream of bytes then restore it
*/
public Person deepCopy() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
oos.flush();
oos.close();
bos.close();
return (Person) new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray())).readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
// Test
Person p1 = new Person("Yahya");
Person p2 = p1;
Person p3 = p1.deepCopy();
// change the original object and check if that affect the others
p1.setName("Uknown");
System.out.println("P1: " + p1.name + " ... P2: " + p2.name
+ " ... P3: " + p3.name);
}
}
Output
P1: Uknown ... P2: Uknown ... P3: Yahya

hibernate: insert instance with external "id class"

I'm using Hibernate + HSQL on JBOSS server, I need to saveOrUpdate() an Object which has an ID represented by another class:
public class RideId implements java.io.Serializable {
private int beginPop;
private int endPop;
private String requestUser;
public RideId() {
}
public RideId(int beginPop, int endPop, String requestUser) {
this.beginPop = beginPop;
this.endPop = endPop;
this.requestUser = requestUser;
}
...
so, "RideID" is the ID of the entity "Ride"
public class Ride implements java.io.Serializable {
private RideId id;
private User userByRequestUser;
private User userByAcceptUser;
private Pop pop;
private Boolean rideStatus;
public Ride() {
}
public Ride(RideId id, User userByRequestUser, Pop pop) {
this.id = id;
this.userByRequestUser = userByRequestUser;
this.pop = pop;
}
public Ride(RideId id, User userByRequestUser, User userByAcceptUser,
Pop pop, Boolean rideStatus) {
this.id = id;
this.userByRequestUser = userByRequestUser;
this.userByAcceptUser = userByAcceptUser;
this.pop = pop;
this.rideStatus = rideStatus;
}
...
how can I saveOrUpdate() a new Object of type Ride?
Thanks everyone and sorry for my english!
It's simple. You need to create first a new RideId, assign it to a new Ride and call saveOrUpdate with the Ride.
RideId id = new RideId(1, 2, "someuser");
Ride ride = new Ride(id, ...);
session.saveOrUpdate(ride);

Categories