`

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

    博客分类:
  • java
JPA 
阅读更多
这个是实体:
@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;
	[color=red]@Lob
	private byte[] image;[/color]
	@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
大家帮我看看怎么回事?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics