Изменить код на:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "dept_id")
private Long deptId;
-setValuesForKeysWithDictionary: метод вместе с -dictionaryWithValuesForKeys :, это то, что вы хотите использовать.
Пример:
// In your custom class
+ (id)customClassWithProperties:(NSDictionary *)properties {
return [[[self alloc] initWithProperties:properties] autorelease];
}
- (id)initWithProperties:(NSDictionary *)properties {
if (self = [self init]) {
[self setValuesForKeysWithDictionary:properties];
}
return self;
}
// ...and to easily derive the dictionary
NSDictionary *properties = [anObject dictionaryWithValuesForKeys:[anObject allKeys]];
Предполагая, что ваш класс соответствует протоколу Key-Value Coding , вы можете использовать следующее: (для удобства определено как категория в NSDictionary):
// myNSDictionaryCategory.h:
@interface NSDictionary (myCategory)
- (void)mapPropertiesToObject:(id)instance
@end
// myNSDictionaryCategory.m:
- (void)mapPropertiesToObject:(id)instance
{
for (NSString * propertyKey in [self allKeys])
{
[instance setValue:[self objectForKey:propertyKey]
forKey:propertyKey];
}
}
И вот как вы бы это использовали:
#import "myNSDictionaryCategory.h"
//...
[someDictionary mapPropertiesToObject:someObject];