FAQ Database Discussion Community


How to annotate hibernate collections with generic types?

java,hibernate,jpa,hibernate-mapping,java-generics
I have a one-to-many class member of type List< GenericType < T > >. What is the recommended way to annotate this member in Hibernate? Morbid details: @Entity public class DvOrdered extends DataValue { @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinTable(name="dv_ordered_other_reference_ranges", joinColumns = @JoinColumn(name = "reference_range_id"), inverseJoinColumns = @JoinColumn(name =...

How can I implement generic method which has generic return type and generic parameter?

java,generics,java-generics
I have a generic interface Service: public interface Service { public <T> T callService(T request); } I want to modify the callService method such that it can accept a generic parameter and return an instance of a different generic return type. How do I do that, either with the above...

Sort a set and assign the content to a List in java

java,interface,implements,java-generics
I have the following java test code that I want to implement: import java.util.*; public class TestRisorseWeb { public static void main(String[] args) { RisorsaWeb p = new PaginaWeb("index.html","<html><body>main</body></html>"); // name and content java.io.File f = new FileWeb("pippo.txt","Questo e' il contenuto\ndi un file di testo."); assert f instanceof RisorsaWeb; Set<RisorsaWeb>...

Why there is no ArrayStoreException thrown in below code?

java,arrays,generics,java-generics
private static void genericArrayTest() { ArrayList<? extends Exception>[] exceptionListArray = new ArrayList[10]; Object[] exceptionObjectListArray = exceptionListArray; ArrayList<String> wrongArrayList = new ArrayList<String>(); wrongArrayList.add("String One"); ArrayList<NullPointerException> exceptionList = new ArrayList<>(); ArrayList rawList = new ArrayList(); exceptionListArray[0] = exceptionList; //exceptionListArray[0] = wrongArrayList; // Compile error? //exceptionListArray[0] = rawList;...

Exception with generic set of ConstraintViolations

java,generics,java-generics
I am working on some service code that is using JSR-303 validation to test a request object. What I would like to do is throw a custom RuntimeException when the validation fails containing the Set of ConstraintViolations. All of my request objects ultimately implement the same interface, lets call it...

Java generic usage error

java,collections,java-generics
The code below uses simple generics and compiles correctly. When I try to create a object of type McDelayQueue, I get an error. public class McDelayQueue<E extends Delayed & IValue<K>, K> extends DelayQueue<E> { final Set keys = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>()); @Override public E take() throws InterruptedException { E data...

Can't figure out how to instantiate generic class returned from function with condition like

java,android,generics,android-studio,java-generics
Can't figure out how to get this work: //Have a tiny CustomServiceLoader implementation public class MyServiceLoader { static private Map<Class<?>, Class<?>> CONTENTS = new HashMap<>(); static public <S, T extends S> void registerClassForInterface(Class<T> c, Class<S> i) { if (c == null) { CONTENTS.remove(i); } else { CONTENTS.put(c, i); } }...

Eclipse tells me that Long is not Comparable

java,types,parametric-polymorphism,java-generics
I am in a strange situation here, namely eclipse tells me that Long is "not a valid substitute for the bounded parameter <T extends Comparable<? super T>>". Any suggestions on what may be the cause? I'm pasting relevant code below abstract Pair: public abstract class Pair<T extends Comparable<? super T>,...

How do I make a list of several kind of object if I cannot use wildcards?

java,bounded-wildcard,java-generics
I want to make a List which can hold two kind of Object. What comes in my mind is to use wildcard. Below is my code. public class Parent { //code } public class ChildOne extends Parent { //code } public class ChildTwo extends Parent { //code } public class...

How to sum values in a Map with a stream?

java,java-8,java-stream,java-generics
I want the equivalent of this with a stream: public static <T extends Number> T getSum(final Map<String, T> data) { T sum = 0; for (String key: data.keySet()) sum += data.get(key); return sum; } This code doesn't actually compile because 0 cannot be assigned to type T, but you get...

How to restrict a class from taking certain types in Java?

java,java-generics
I have a base class Product that has five subclasses (ComputerPart, Peripheral, Service, Cheese, Fruit). Each of these further have 2/3 subclasses. Then I have a GenericOrder class that acts as a collection of an arbitrary number of objects in the Product class. GenericOrder has a subclass called ComputerOrder that...

Erasing Collections generics and conflicting overloads

java,generics,type-erasure,java-generics
I have a rather central class responsible for some Utility methods. I wanted to provide an additional overload for a method which was declared as follows: public static void removeDuplicated(Collection fullList, Collection itemsToRemove) { //implementation stripped for brevity } Now since I don't really like Rawtypes, I specified the collections...