jpa blob mysql_jpa对blob类型的支持,出现错误

这个是实体:

@Entity

@Table(name="Person3")

public class Person3 implements Serializable {

public enum Level{LEVEL1,LEVEL2,LEVEL3};

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

@Column(name="username",unique=true,nullable=false)

private String name;

private int age;

private double salary;

@Lob

private byte[] image;

@Temporal(TemporalType.DATE)

private Date birthday;

@Column(name="isMarried",columnDefinition="tinyint(1)")

private boolean isMarried;

@Enumerated(EnumType.STRING)

private Level level;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

public byte[] getImage() {

return image;

}

public void setImage(byte[] image) {

this.image = image;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public boolean isMarried() {

return isMarried;

}

public void setMarried(boolean isMarried) {

this.isMarried = isMarried;

}

public Level getLevel() {

return level;

}

public void setLevel(Level level) {

this.level = level;

}

public Person3() {

}

public Long getId() {

return this.id;

}

public void setId(Long id) {

this.id = id;

}

public int hashCode() {

return (this.id == null) ? 0 : this.id.hashCode();

}

public boolean equals(Object object) {

if (object instanceof Person3) {

final Person3 obj = (Person3) object;

return (this.id != null) ? this.id.equals(obj.id)

: (obj.id == null);

}

return false;

}

}

这个是测试类:

private static void main() throws Exception {

Context ctx = new InitialContext();

PersonManageBeanRemote pmb = (PersonManageBeanRemote) ctx.lookup("PersonManageBean/remote");

Person3 p = new Person3();

p.setName("lili");

p.setAge(23);

p.setMarried(true);

p.setLevel(Person3.Level.LEVEL1);

p.setSalary(2000.0);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

try {

p.setBirthday(sdf.parse("1980-08-08"));

} catch (ParseException e) {

e.printStackTrace();

}

//把图片保存到数据库

InputStream is = Test.class.getClassLoader().getResourceAsStream("holly-11-10c.gif");

byte[] buf=new byte[is.available()];

is.read(buf);

System.out.println("\n===="+buf.length);

is.close();

p.setImage(buf);

pmb.save(p);

Person3 p2 = pmb.findPerson("lili");

System.out.println(p2.getSalary()+"=="+p2.getBirthday());

byte[] image = p2.getImage();

System.out.println("\n----"+image.length);

//把图片从数据库中读出来

FileOutputStream fos2 = new FileOutputStream("temp2.gif");

fos2.write(image);

fos2.close();

}

各位:最后对出来的图片不对,比原来的字节数多了8个

打印结果:

====1473

2000.0==1980-08-08

----1481

大家帮我看看怎么回事?

2009年1月09日 10:47

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值