Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
These are the two tables CompositeDTO,CompositeKeyDTO.
Using reflection I want to save into database.
The database structure is:
SQL
CREATE TABLE public.test_jdbc_composite
(
  id uuid NOT NULL,
  num character varying(50) NOT NULL,
  title character varying(150),
  date_add timestamp without time zone,
  date_update timestamp without time zone,
  CONSTRAINT test_jdbc_composite_pkey PRIMARY KEY (id, num)
)
It is giving error because id, num belong to CompositeKeyDTO and we have entity class of CompositeDTO.
I want both fields and values so that I can save into database.

What I have tried:

Java
@Getter
@Setter
@Table(name="test_jdbc_composite")
public class CompositeDTO extends GenericDTO<compositekeydto> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6200885628682542590L;
	
	@Key
	private CompositeKeyDTO key;	
	@Column
	private String title;
}

    @Getter
@Setter
@Table(name="test_jdbc_composite")
public class CompositeKeyDTO implements KeyDTO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4414037231838241192L;
	@Column
	private UUID id;
	@Column
	private String num;

	public UUID getId() {
		return id;
	}
	public void setId(UUID id) {
		this.id = id;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
}
I get all the fields using this reflection:
Java
private static void getClassFields(final Class c, final LinkedList<field> fields) {
		for (Field f : c.getDeclaredFields()) {
			if (Modifier.isStatic(f.getModifiers())) {
				continue;
			}
			if (f.getType().getName().contains("lc.stark.frw.persistence.jdbc")) {
				try {
					getClassFields(Class.forName(f.getType().getName()), fields);
				} catch (ClassNotFoundException e) { // TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {

				fields.add(f);
			}
		}
	}
 Field[] field = DynamicFieldUtilsJdbc.getClassFields( Class c,  LinkedList<field> fields));
		Object value;
		
        for (int i = 0; i < field.length; i++) {
		field[i] = "get" + field[i] .substring(0, 1).toUpperCase() + field[i] .substring(1);
		Object value = null;
        Method m = entity.getClass().getMethod(field[i] , null);
			value = m.invoke(entity);
Posted
Updated 3-Jan-20 1:20am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900