lundi 29 décembre 2014

jpa composite key with embedded field reflection fail

I want to have a primary key with an embedded field, i have managed to have hibernate create the tables, but it is having a problem with saving, when i save (using spring repository), i get the following exceptions:


"org.hibernate.PropertyAccessException: could not get a field value by reflection getter of StockHistory.dateTime"


and down the stack trace there is the exception


"java.lang.IllegalArgumentException: Can not set utils.time.Time field StockHistory.dateTime to utils.time.Time"


this is the primary ID class



public class StockHistoryId3 implements Serializable
{
private static final long serialVersionUID = 1L;

private int stockId;
private Time dateTime;

public StockHistoryId3(){}

public StockHistoryId3(Time time, int stockId)
{
this.setDateTime(time);
this.setStockId(stockId);
}

@Override
public boolean equals(Object target)
{
boolean result = false;

if(target != null && target instanceof StockHistoryId3)
{
StockHistoryId3 other = (StockHistoryId3) target;

if(Integer.compare(this.getStockId(), other.getStockId()) == 0
&& this.getDateTime().equals(other.getDateTime()))
{
result = true;
}
}

return result;
}

@Override
public int hashCode()
{
int result = 89;

result += this.getStockId() + this.getDateTime().getDateTime();

return result;
}

// setters & getters
public int getStockId()
{
return stockId;
}

private void setStockId(int stockId)
{
this.stockId = stockId;
}

public Time getDateTime()
{
return dateTime;
}

public void setDateTime(Time dateTime)
{
this.dateTime = dateTime;
}
}


and this is the class to be mapped (not all of it for brevity)



@Entity
@Table(name = Configuration.DB_TABLE_NAME_STOCK_HISTORY)
@IdClass(StockHistoryId3.class)
public class StockHistory
{
@Id
@Column(name = "stockId", insertable = false, updatable = false)
@JoinColumn(name = "stockId", nullable = false)
private int stockId;

@Id
@Embedded
@AttributeOverrides
({
@AttributeOverride(name="timestamp", column=@Column(name="time")),
})
private Time dateTime;
}


and this is the Time class (not all of it for brevity)



@Embeddable
@Component(value = "Time")
@Scope(value = "prototype")
public class Time implements Serializable
{
private static final long serialVersionUID = 1L;


@Column(name = "timestamp")
private long dateTime;

@Transient
private LocalDateTime date;


public Time(){}

public Time(long timestamp)
{
this.setDateTime(timestamp);
this.setDate(new LocalDateTime(timestamp));
}

@Override
public boolean equals(Object target)
{
boolean result = false;

if(target != null && target instanceof Time)
{
Time other = (Time) target;

if(this.getDateTime() == other.getDateTime())
{
result = true;
}
}

return result;
};
}


any help about my problem thanks






Aucun commentaire:

Enregistrer un commentaire